下面的视频是我的操作步骤,将表格里的内容复制,然后依次把文档的文件名替换掉,打开文档然后替换掉文档的标题,和开头部分内容。
把Excel工作簿复制到Word文档所在文件夹下,打开后插入模块,粘贴以下代码,点击运行
Sub test1()
Application.ScreenUpdating = False
Dim wdapp As Word.Application, wdoc As Document, r&, myp$, myf$, i&, a$, b$, c$
Set wdapp = New Word.Application
myp = ThisWorkbook.Path & "\"
myf = Dir(myp & "*.docx")
Do While myf <> ""
i = i + 1
a = Cells(i + 1, 3).Text
If a = "" Then Exit Do
Set wdoc = wdapp.Documents.Open(myp & myf)
b = wdoc.Paragraphs(1).Range.Text
b = Left(b, Len(b) - 1)
wdapp.Selection.Find.ClearFormatting
wdapp.Selection.Find.Replacement.ClearFormatting
With wdapp.Selection.Find
.Text = b
.Replacement.Text = a
End With
wdapp.Selection.Find.Execute Replace:=wdReplaceAll
wdapp.Documents.Close True
c = myp & a & ".docx"
Name myp & myf As c
myf = Dir
Loop
wdapp.Quit
Set wdapp = Nothing
Set wdoc = Nothing
Application.ScreenUpdating = True
End Sub
注:勾选蓝色部分选项