将图片插入word文档

时间:2014-10-01 11:59:50

标签: .net vb.net templates ms-word .doc

您好我希望将图片从图片框插入到.doc文件中。我能够将图像放入文件中并保存正确但我在放置图像时遇到问题。我希望将图像放在模板.doc中的表中,但它总是在第一个表的第一个单元格中结束。

    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

1 个答案:

答案 0 :(得分:1)

嗨,这是我的工作代码

  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()