在word文档中嵌入多个excel文件,显示为图标

时间:2014-02-20 08:37:33

标签: vb.net excel ms-word

我可以将单个excel文件复制到word文档中,显示为图标。但是当我从文件夹中选择多个文件时,会发生的情况是每个文件都被复制到一个新的word文档中。换句话说,我无法一次将多个文件嵌入到单个word文档中。

此外,在我的代码中,我必须从开始进入word文档创建书签。我想通过代码动态地在word文档中创建书签。

有没有人可以指导我如何通过代码动态创建书签,以及如何将多个文件一次嵌入到单个word文档中。

这是我的工作代码,一次只嵌入一个文件。

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim ofd As New OpenFileDialog
    ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    ofd.Filter = "Excel Files (*.xls;xlsx)|*.xls;xlsx"
    ofd.FilterIndex = 2
    ofd.RestoreDirectory = True
    ofd.Multiselect = True

    Dim Files As String = ofd.InitialDirectory
    If ofd.ShowDialog() = DialogResult.OK Then

        For Each Files In ofd.FileNames

            Dim filename As String = ofd.FileName

            Dim oWord As Word.Application = New Word.Application()
            Dim oWordDoc As Word.Document = oWord.Documents.Open("C:\testing.doc", False)
            Dim oMissing As Object = System.Reflection.Missing.Value
            oWord.Visible = True


            'ICON LABEL CAN BE THE NAME OF THE FILE, 
            'ITS THE NAME DISPLAYED BESIDES THE EMBEDDED DOCUMENT

            Dim oIconLabel As Object = filename                


            'THE BOOKMARK WHERE THE FILE NEEDS TO BE EMBEDDED
            Dim oBookMark As Object = "ssss"
            '//THE LOCATION OF THE FILE
            Dim oFileDesignInfo As Object = filename


            '//OTHER VARIABLES
            Dim oClassType As Object = "Word.Document.8"
            Dim oTrue As Object = True
            Dim oFalse As Object = False
            'Dim oMissing As Object = System.Reflection.Missing.Value
            Dim oIconFileName As Object = oMissing

            'METHOD TO EMBED THE DOCUMENT

            oWordDoc.Bookmarks.Item(oBookMark).Range.InlineShapes.AddOLEObject(oClassType, oFileDesignInfo, _
            oFalse, oTrue, oIconFileName, oMissing, oIconLabel, oMissing)

            Next
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

用于打开新单词实例并加载文档的代码在循环内 - 将其移出循环,因此您只需运行一次。

Private Sub Button1_Click(ByVal sender As System.Object, _
                 ByVal e As System.EventArgs) Handles Button1.Click

    Dim ofd As New OpenFileDialog
    ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    ofd.Filter = "Excel Files (*.xls;xlsx)|*.xls;xlsx"
    ofd.FilterIndex = 2
    ofd.RestoreDirectory = True
    ofd.Multiselect = True

    Dim Files As String = ofd.InitialDirectory
    If ofd.ShowDialog() = DialogResult.OK Then

        Dim oWord As Word.Application = New Word.Application()
        Dim oWordDoc As Word.Document = oWord.Documents.Open("C:\testing.doc", False)
        Dim oMissing As Object = System.Reflection.Missing.Value
        oWord.Visible = True

        For Each Files In ofd.FileNames
            '*************
            'rest of code....
            '*************
        Next
    End If
End Sub

答案 1 :(得分:0)

最后成功完成了......这是工作代码:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim ofd As New OpenFileDialog
    ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    ofd.Filter = "Excel Files (*.xls;xlsx)|*.xls;xlsx"
    ofd.FilterIndex = 2
    ofd.RestoreDirectory = True
    ofd.Multiselect = True

    Dim Files As String

    If ofd.ShowDialog() = DialogResult.OK Then

        Dim oWord As Word.Application = New Word.Application()
        Dim oWordDoc As Word.Document = oWord.Documents.Open("C:\testing.doc", True)
        Dim oBookmark As Object = oWordDoc.Bookmarks.Add("Bookmark")
        Dim oMissing As Object = System.Reflection.Missing.Value
        oWord.Visible = True

        For Each Files In ofd.FileNames

            'ICON LABEL CAN BE THE NAME OF THE FILE, 
            'ITS THE NAME DISPLAYED BESIDES THE EMBEDDED DOCUMENT
            Dim oIconLabel As Object = Path.GetFileName(Files)

            '//INCASE WE NEED THE EMBEDDED DOCUMENT TO BE DISPLAYED AS A SPECIFIC ICON,
            '//WE NEED TO SPECIFY THE LOCATION OF THE ICON FILE
            '//ELSE SET IT TO oMissing VALUE
            'Dim oIconFileName As Object = "C:\\Document and Settings\\IconFile.ico"

            '//THE LOCATION OF THE FILE
            Dim oFileDesignInfo As Object = Files

            '//OTHER VARIABLES
            Dim oClassType As Object = "Word.Document.8"
            Dim oTrue As Object = True
            Dim oFalse As Object = False
            'Dim oMissing As Object = System.Reflection.Missing.Value
            Dim oIconFileName As Object = oMissing

            '//METHOD TO EMBED THE DOCUMENT

            oWordDoc.Bookmarks.Item(oBookMark).Range.InlineShapes.AddOLEObject(oClassType, oFileDesignInfo, _
            oFalse, oTrue, oIconFileName, oMissing, oIconLabel, oMissing)

        Next
    End If

    Me.Close()
End Sub