使用VB合并word文档内容

请教各位VB合并word文档的编写方法。有多个word文档,如01.docx,02.docx,03.docx等等多个word文档。

img

现在需要根据filelist.txt中word文档的文件名称,filelist.txt中的文档名称不固定,可能是01、02,下次可能是01、02、03等,把txt文本中word文档全部合并到一个新的word文档中。

img

用Word.Application来创建新的docx,然后将需要的docx文档一个个读入。
参考下面的代码,确保filelist.txt最后没有空行。

Imports Microsoft.Office.Interop
Imports System.IO
        Dim filepath As String = "你的文件目录"
        Dim filelist() As String = Split(File.ReadAllText(filepath + "filelist.txt"), vbCrLf)
        Dim WordTemps As New Word.Application
        WordTemps.Documents.Add()
        For Each filename In filelist
            Console.WriteLine(filename)
            WordTemps.Selection.InsertFile(filepath + filename, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False)
        Next
        WordTemps.Visible = False
        WordTemps.ActiveDocument.SaveAs2(filepath + "合并文档.docx")
        WordTemps.Quit()

看看这篇文章:C#/VB.NET 如何合并 Word 文档