3
votes

I want to insert an in memory image (bitmap) into a Word document programmatically.

That is, I create a Word document in memory via Microsoft.Office.Interop.Word. Then I want to append a series of bitmap images into the document one after another and save to a .doc file.

I have been searching online but the only solutions I’ve found are

1) Add the image to the ClipBoard and then paste the selection into the open document using Microsoft.Office.Interop.Word.Selction.Paste().

2) Write the image out to a file and insert it using Microsoft.Office.Interop.Word.InlineShapes.AddPicture().

While both of these methods work, I find them to be very awkward especially the first option which takes control of the clipboard while the application is running.

Is there any other way to do this?

2

2 Answers

1
votes

The only other possibility would be to insert the image via the Range.InsertXML method. This would mean the image needs to be in base64 and wrapped in valid WordOpenXML using the OPC Flat File format.

Word Open XML is the only way to "stream" information into a Word document.

0
votes

You can use InsertFile over the Selection or specific Range object.

NOTE: This also will require the bitmap to be saved on the hard drive. I think the only in memory add of image can be done with Copy / Paste using the clipboard.