current position:Home>Merging word document content with VB

Merging word document content with VB

2022-02-03 01:42:17 CSDN Q & A

May I have your advice VB Merge word How to write the document . There are many. word file , Such as 01.docx,02.docx,03.docx Wait a few word file .

img

Now we need to according to filelist.txt in word File name of the document ,filelist.txt The document name in is not fixed , May be 01、02, Next time it could be 01、02、03 etc. , hold txt In the text word Merge all documents into a new word In the document .

img




Refer to the answer 1:

use Word.Application To create a new docx, Then you will need docx Documents are read in one by one .
Refer to the following code , Make sure filelist.txt Finally, there is no blank line .

Imports Microsoft.Office.InteropImports System.IO        Dim filepath As String = " Your file directory "        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 + " Merge documents .docx")        WordTemps.Quit()



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030142150552.html

Random recommended