复制
收藏
提问
简洁
编写合并word文档的VBA脚本程序
2个月前
全网
文档
学术
百科
知识库
回答简洁模式
深入研究一些
编写合并Word文档的VBA脚本程序
- 准备工作:将所有待合并文档放入同一个文件夹中,并准备好一个空白的Word文档作为合并后的文件。147
- 编写VBA脚本:以下是一个简单的VBA脚本示例,用于将同一文件夹中的多个Word文档合并到一个新的Word文档中。
Sub MergeDocuments()
Dim fileName As String
Dim doc As Document
Dim path As String
' 设置文件夹路径
path = "C:\Documents\"
' 创建一个新的Word文档
Set doc = Documents.Add
' 遍历文件夹中的所有Word文档
fileName = Dir(path & "*.docx")
Do While fileName <> ""
' 打开文档并将其内容复制到新文档中
Documents.Open (path & fileName)
Selection.WholeStory.Copy
doc.Windows(1).Selection.WholeStory.Paste
' 添加分页符
doc.Content.InsertBreak Type:=wdPageBreak
' 关闭打开的文档
Documents.Close SaveChanges:=0
fileName = Dir()
Loop
' 保存合并后的文档
doc.SaveAs "MergedDocument.docx"
doc.Close SaveChanges:=0
End Sub
你觉得结果怎么样?