Search code examples
.netvb.nettemplatesms-word.doc

Inserting a picture to a word document


Hi I wish to insert an image form a picturebox into a .doc file. I am able to get the image into the file and it saves correctly but I am having trouble with the placement of the image. I want to have the image in a table that exists on the template .doc but it always ends up in the first cell on the first table.

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim objWordApp As Word.Application
        objWordApp = New Word.Application
        Dim objDoc As Word.Document

        objWordApp.Documents.Open("" & Form2.TextBox1.Text & "\capatext\temlate.doc")
        objDoc = objWordApp.ActiveDocument

        'Open an existing document.


        objDoc.Content.Find.Execute(FindText:="<<srce>>", _
ReplaceWith:=Form1.TextBox15.Text, _
Replace:=Word.WdReplace.wdReplaceAll) ''''''
        Dim oldbitmap As Bitmap = New Bitmap(Form1.PictureBox1.Image)
        Dim newbitmap As Bitmap = New Bitmap(CInt(100%), CInt(100%))
        Dim g As Graphics = Graphics.FromImage(newbitmap)
        g.DrawImage(oldbitmap, 0, 0, newbitmap.Width, newbitmap.Height)
        Dim pathToSavedImage As String = "" & Form2.TextBox1.Text & "\capatext\tempimg1.jpg"
        newbitmap.Save(pathToSavedImage)
        oldbitmap.Dispose()
        newbitmap.Dispose()
        g.Dispose()

        Dim ObjPic As Microsoft.Office.Interop.Word.InlineShape = objDoc.InlineShapes.AddPicture(pathToSavedImage)
        Dim oldCopy As String
        oldCopy = ("" & Form2.TextBox1.Text & "\" & Form1.TextBox1.Text & ".doc")
        If System.IO.File.Exists(oldCopy) = True Then
            System.IO.File.Delete(oldCopy)
        ElseIf System.IO.File.Exists(oldCopy) = False Then
        End If
        objWordApp.Quit()
        objWordApp = Nothing
        Timer1.Start()
    End Sub

Solution

  • Hi here is my working code

      If Form1.CheckBox4.CheckState.Equals(CheckState.Checked) Then
    
                Dim oldbitmap As Bitmap = New Bitmap(Form1.PictureBox1.Image)
                Dim newbitmap As Bitmap = New Bitmap(CInt(100%), CInt(100%))
                Dim g As Graphics = Graphics.FromImage(newbitmap)
                g.DrawImage(oldbitmap, 0, 0, newbitmap.Width, newbitmap.Height)
                Dim pathToSavedImage As String = "" & Form2.TextBox1.Text & "\capatext\tempimg1.jpg"
                newbitmap.Save(pathToSavedImage)
    
                oldbitmap.Dispose()
                newbitmap.Dispose()
                g.Dispose()
    
                objDoc.Tables(1).Cell(4, 1).Range.InlineShapes.AddPicture( _
                FileName:=pathToSavedImage, LinkToFile:=False)
            Else
            End If
            Dim oldCopy As String
            oldCopy = ("" & Form2.TextBox1.Text & "\" & Form1.TextBox1.Text & ".doc")
            If System.IO.File.Exists(oldCopy) = True Then
                System.IO.File.Delete(oldCopy)
            ElseIf System.IO.File.Exists(oldCopy) = False Then
            End If
            objWordApp.Documents.Item(1).Save()
            objWordApp.Quit()
            objWordApp = Nothing
    
            Timer1.Start()