locked
Insert an image to Word RRS feed

  • Question

  • Using vb.net 2008, I convert a base64 xml image to byte array, memory stream or image…
    How can I now insert this into a word document, programmatically, without saving it to a file or clipboard first?

    Tuesday, November 11, 2008 4:06 PM

Answers

  • You can use ActiveDocument.InlineShapes.AddPicture for inserting images. For more details on method AddPicture, please visit AddPicture method as it applies to the InlineShapes object.

    Hope this will help.
    • Marked as answer by Riquel_Dong Monday, November 17, 2008 2:12 AM
    Wednesday, November 12, 2008 6:18 AM
  • You need to take a look at Image.FromStream method in this scenario.

    Imports Microsoft.Office.Interop.Word
    Public Class Form6
        Dim WordApp As New Microsoft.Office.Interop.Word.Application
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim filename As Object = "C:\insertimg.docx"
            Dim newdoc As Document = WordApp.Documents.Open(filename)
            Dim oBarcodeBookmark As Object = "bookmark1"
            Dim save As Object = False
            Dim destFile As Object = "C:\afterinsert.doc"
            Dim range As Microsoft.Office.Interop.Word.Range
            range = newdoc.Bookmarks.Item(oBarcodeBookmark).Range
            range.Select()
            WordApp.Selection.TypeParagraph()
            Dim bi As Image = Image.FromFile("c:\2.bmp")
            Clipboard.SetDataObject(bi)
            range.Paste()
            range.Select()
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
            newdoc.SaveAs(destFile)
            WordApp.Quit(save)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp)
            bi.Dispose()
            MessageBox.Show("successfully inserted image")
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Riquel_Dong Monday, November 17, 2008 2:12 AM
    Friday, November 14, 2008 9:34 AM

All replies

  • You can use ActiveDocument.InlineShapes.AddPicture for inserting images. For more details on method AddPicture, please visit AddPicture method as it applies to the InlineShapes object.

    Hope this will help.
    • Marked as answer by Riquel_Dong Monday, November 17, 2008 2:12 AM
    Wednesday, November 12, 2008 6:18 AM
  • Thanks Anuj for your response but I was hoping not to save the image to a file....but it looks like I'm going to have to do that.
    Wednesday, November 12, 2008 6:43 PM
  • You need to take a look at Image.FromStream method in this scenario.

    Imports Microsoft.Office.Interop.Word
    Public Class Form6
        Dim WordApp As New Microsoft.Office.Interop.Word.Application
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim filename As Object = "C:\insertimg.docx"
            Dim newdoc As Document = WordApp.Documents.Open(filename)
            Dim oBarcodeBookmark As Object = "bookmark1"
            Dim save As Object = False
            Dim destFile As Object = "C:\afterinsert.doc"
            Dim range As Microsoft.Office.Interop.Word.Range
            range = newdoc.Bookmarks.Item(oBarcodeBookmark).Range
            range.Select()
            WordApp.Selection.TypeParagraph()
            Dim bi As Image = Image.FromFile("c:\2.bmp")
            Clipboard.SetDataObject(bi)
            range.Paste()
            range.Select()
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight
            newdoc.SaveAs(destFile)
            WordApp.Quit(save)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp)
            bi.Dispose()
            MessageBox.Show("successfully inserted image")
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Riquel_Dong Monday, November 17, 2008 2:12 AM
    Friday, November 14, 2008 9:34 AM