|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
编写了以下代码,想对指定文件夹下的多个word进行特定文本的批量替换,但不起作用,求助大佬指出代码错误之处?
- Sub test()
- Dim glkFileDialog As FileDialog '定义选择文件对话框
- Dim glkSelectedItem As Variant '定义变量
- Dim glkDoc As Document '定义文档
- Set glkFileDialog = Application.FileDialog(msoFileDialogFilePicker) '设置变量
- With glkFileDialog '设置对话框属性
- .Filters.Clear '清除过滤器
- .Filters.Add "所有Word文件", "*.Docx", 1 '添加过滤条件
- .AllowMultiSelect = True '允许多选
- If .Show = -1 Then '如果按 【打开】
- Application.ScreenUpdating = False '关闭屏幕更新
- For Each glkSelectedItem In .SelectedItems 'For - Next循环
- Set glkDoc = Documents.Open(FileName:=glkSelectedItem, Visible:=False) '后台打开所选文档
- Selection.Find.ClearFormatting
- Selection.Find.Replacement.ClearFormatting
- 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
- glkDoc.Save
- glkDoc.Close
-
- Next
- Application.ScreenUpdating = True '启用屏幕更新
- MsgBox "所选文档替换完毕!", vbInformation
- Else '如果按 【取消】
- MsgBox "您取消了本次操作!", vbInformation
- End If
- End With
- End Sub
复制代码
|
|