Ukkie
Ukkie

Reputation: 25

How to add two pictures next to eachother in a line to a word file with vb.net

I want to place 2 pictures next to eachother in a wordfile. The pictures are already in vb.net as an image or a byte() array.

I see 2 possibilities for placing pictures into the wordfile:

the first methode I stopped investigating while it always starts from a picture file on the harddrive and I do not see a methode to load an image or byte() array.. Anybody a solution for that (instead of saving to a local file and loading it back..)

with the second methode I can not seem to place the pictures next to eachother.. It looks like the picture (with text ) seems to be selected when the second picture is pasted.. As you see in the script I tried some stuff to unselect the first picture but it does not seem to work.. The "Insertafter" or "moveEnd" statement does not help..

Anybody an idea?

        sparePicture = ByteArrayToImage(lstOrderedSpares(1)) 
        Clipboard.SetDataObject(sparePicture)
        Dim oPara4 = oDoc.Content.Paragraphs.Add(oMissing)

        oPara4.Range.Paste()
        oPara4.Range.InsertAfter("hoi")
        oPara4.Range.InsertAfter("hoi2")
        oPara4.Range.MoveEnd()
        
        
        sparePicture = ByteArrayToImage(lstOrderedSpares(2)) 
        Clipboard.SetDataObject(sparePicture)
        oPara4.Range.Paste()
        

EDIT:

What I did as workaround, I used a table... I still think it is interesting that I can not find a direct methode.

You have to use With....end with statements, otherwise it did not work... (I do not use them a lot normally...)

        Dim oPara6 = oDoc.Content.Paragraphs.Add(oMissing)
    oPara6.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft

    Dim tableLocation1 As Word.Range = oPara6.Range()
    oDoc.Tables.Add(Range:=tableLocation1, NumRows:=1, NumColumns:=3)


    sparePicture = ByteArrayToImage(lstOrderedSpares(1))
    Clipboard.SetDataObject(sparePicture)
    With oDoc.Tables.Item(3).Cell(1, 1).Range
        .Paste()
        .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    End With

    sparePicture = ByteArrayToImage(lstOrderedSpares(2))
    Clipboard.SetDataObject(sparePicture)
    With oDoc.Tables.Item(3).Cell(1, 2).Range
        .Paste()
        .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    End With

    sparePicture = ByteArrayToImage(lstOrderedSpares(3))
    Clipboard.SetDataObject(sparePicture)
    With oDoc.Tables.Item(3).Cell(1, 3).Range
        .Paste()
        .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    End With

Upvotes: 0

Views: 41

Answers (0)

Related Questions