标签 :VBA 批处理 word ,使用宏批量替换内容。
word 宏操作 批处理文档
问题描述 :有一系列的word文档,想把每份文档里面的名字A换成名字B。
方法步骤 :
运行VBA:
程序需要改动的地方有两个:
- 文件夹路径
- 个人宏名
c
Sub 批量操作WORD()
Dim path As String
Dim FileName As String
Dim worddoc As Document
Dim MyDir As String
MyDir = "D:\Desktop\19#411(2)\19#411" '文件夹路径根据需要自己修改,需要处理的文件都放该文件夹内
FileName = Dir(MyDir & "\*.doc*", vbNormal)
Do Until FileName = ""
If FileName <> ThisDocument.Name Then
Set worddoc = Documents.Open(MyDir & "\" & FileName)
worddoc.Activate
Call 宏4 '调用宏,换成你自己宏的名字
worddoc.Close True
FileName = Dir()
End If
Loop
Set worddoc = Nothing
End Sub
====================== 下面的宏换成你自己的宏=================================
c
Sub 宏4()
'
' 宏4 宏
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "411"
.Replacement.Text = "325"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "李得"
.Replacement.Text = "居美"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub