分享

使用Office内置的VBA编辑器实现WORD文档的批量查找替换

 昵称163835 2022-07-26 发布于广东

最近同事因为工作原因需要批量修改几百个WORD文档中的内容,并且是批量的重复性工作。如果按一个个文件打开,是一个繁琐、乏味且又容易遗漏出错的事儿,所以他想找一个能提供批量替换操作的工具,百度上有很多类似的Office插件,但都是收费的。

鉴于所有此类的工具都是基于Office提供的自动化完成的,所以我使用了Word内置的VBA编辑器编写了宏来实现这一功能。

首先在Word的文件->选项->自定义功能区中勾选右侧“主选项卡”中的“开发工具”后点击确定。

然后选择“开发工具”选项卡下的“Visual Basic”。

点击“插入”菜单中的“用户窗体”

 按下图添加控件,并修改对应的文本属性

 双击控件添加代码如下

  1. Public filecount As Integer '文件总数
  2. Public proccount As Integer '已经打开的数量
  3. Public recount As Integer '已替换的数量
  4. '替换文本
  5. Function WordReplaces(FileName As String, SearchString As String, ReplaceString As String)
  6. On Error Resume Next
  7. Dim wdoc As Document
  8. Set wdoc = Word.Documents.Open(FileName)
  9. Dim wrnd As Range
  10. Set wrnd = wdoc.Range
  11. wrnd.Select
  12. With wrnd.Find
  13. .Text = SearchString
  14. .MatchCase = False
  15. .Wrap = wdFindContinue
  16. .Replacement.Text = ReplaceString
  17. End With
  18. If wrnd.Find.Execute(, , , , , , , , , , wdReplaceAll) Then
  19. recount = recount + 1
  20. End If
  21. wdoc.Save
  22. wdoc.Close
  23. End Function
  24. '枚举文件
  25. Function FilesTree(ByVal sPath As String, ByVal sSearch As String, ByVal sReplace As String)
  26. On Error Resume Next
  27. Set oFso = CreateObject('Scripting.FileSystemObject')
  28. Set oFolder = oFso.GetFolder(sPath)
  29. Set oSubFolders = oFolder.SubFolders
  30. Set oFiles = oFolder.Files
  31. For Each oFile In oFiles
  32. If UCase(Right(oFile.Name, 5)) = '.DOCX' Or UCase(Right(oFile.Name, 4)) = '.DOC' Then
  33. proccount = proccount + 1
  34. Me.Label5.Caption = oFile.Path
  35. Me.Label6.Caption = Str(proccount)
  36. Label8.Width = proccount / filecount * Me.Frame3.Width
  37. Label8.Caption = Str(Int(proccount / filecount * 100)) & '%'
  38. Label8.TextAlign = fmTextAlignCenter
  39. Label12.Caption = Str(recount)
  40. DoEvents
  41. Call WordReplaces(oFile.Path, sSearch, sReplace)
  42. DoEvents
  43. End If
  44. Next
  45. For Each oSubFolder In oSubFolders
  46. Call FilesTree(oSubFolder.Path, sSearch, sReplace)
  47. Next
  48. Set oFolder = Nothing
  49. Set oSubFolders = Nothing
  50. Set oFso = Nothing
  51. End Function
  52. '计算总文件数
  53. Function EnumFilesCount(ByVal sPath As String)
  54. On Error Resume Next
  55. Set oFso = CreateObject('Scripting.FileSystemObject')
  56. Set oFolder = oFso.GetFolder(sPath)
  57. Set oSubFolders = oFolder.SubFolders
  58. Set oFiles = oFolder.Files
  59. For Each oFile In oFiles
  60. If UCase(Right(oFile.Name, 5)) = '.DOCX' Or UCase(Right(oFile.Name, 4)) = '.DOC' Then
  61. filecount = filecount + 1
  62. End If
  63. Next
  64. For Each oSubFolder In oSubFolders
  65. Call EnumFilesCount(oSubFolder.Path)
  66. Next
  67. Set oFolder = Nothing
  68. Set oSubFolders = Nothing
  69. Set oFso = Nothing
  70. End Function
  71. '打开目录
  72. Private Sub CommandButton1_Click()
  73. On Error Resume Next
  74. Dim objpath As String
  75. Set objshell = CreateObject('Shell.Application')
  76. Set objfolder = objshell.BrowseForFolder(0, '选择一个文件夹', 0)
  77. Set objfolderitem = objfolder.Self
  78. objpath = objfolderitem.Path
  79. If objpath <> '' Then
  80. TextBox1.Text = objpath
  81. End If
  82. End Sub
  83. '替换
  84. Private Sub CommandButton2_Click()
  85. If Trim(Me.TextBox1.Text) = '' Or Me.TextBox2.Text = '' Then
  86. MsgBox '文件夹路径和查找的内容不能为空!'
  87. Else
  88. filecount = 0
  89. proccount = 0
  90. recount = 0
  91. EnumFilesCount (Me.TextBox1.Text)
  92. Me.Label6.Caption = '0'
  93. Me.Label10.Caption = Str(filecount)
  94. Call FilesTree(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text)
  95. MsgBox '替换完成'
  96. End If
  97. End Sub

点击工具栏运行按钮

 现在您就可以轻松快速的完成批量替换操作啦!

 已经编辑好的WORD宏文档也可在我的资源中下载。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多