跳到主要内容

如何从 Excel 中查找和替换 Word 文档中的文本?

在 Word 文档中,我们可以应用查找和替换功能快速查找和替换一个文本。 但是,如果需要查找和替换多个文本,将文本逐个输入到查找和替换功能中会很耗时。 在这种情况下,您可以将查找和替换文本输入到单元格列表中,并在 Excel 中的 VBA 代码的帮助下轻松完成这项工作。 在本文中,我还将介绍一个有用的功能,可以批量查找和替换多个 Word 文档中的文本。

使用 VBA 代码从 Excel 中查找和替换一个 Word 文档中的多个文本

使用 VBA 代码从 Excel 中查找和替换多个 Word 文档中的多个文本

使用强大的功能查找和替换多个 Word 文档中的多个文本


使用 VBA 代码从 Excel 中查找和替换一个 Word 文档中的多个文本

如果您只想在一个 Word 文件中查找和替换某些文本,下面的 VBA 代码可以帮到您。

1. 在 Excel 工作表中,创建一个包含要查找和替换的文本的列,以及另一个包含要替换的文本的列,如下图所示。 然后按 Alt + F11 同时打开 Microsoft Visual Basic应用程序 窗口。

2。 然后,点击 插页 > 模块,将以下 VBA 代码复制并粘贴到窗口中。

VBA 代码:在一个 Word 文件中查找和替换多个文本

Sub replace_texts_range_of_cells()
'Updateby ExtendOffice
Dim xWordApp As Word.Application
Dim xDoc As Word.Document
Dim xRng As Range
Dim I As Integer
Dim xFileDlg As FileDialog
On Error GoTo ExitSub
Set xFileDlg = Application.FileDialog(msoFileDialogFilePicker)
xFileDlg.AllowMultiSelect = False
xFileDlg.Filters.Add "Word Document", "*.docx; *.doc; *.docm"
xFileDlg.FilterIndex = 2
If xFileDlg.Show <> -1 Then GoTo ExitSub
Set xRng = Application.InputBox("Please select the lists of find and replace texts (Press Ctrl key to select two same size ranges):", "Kutools for Excel", , , , , , 8)
If xRng.Areas.Count <> 2 Then
  MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "Kutools for Excel"
  GoTo ExitSub
End If
If (xRng.Areas.Item(1).Rows.Count <> xRng.Areas.Item(2).Rows.Count) Or _
  (xRng.Areas.Item(1).Columns.Count <> xRng.Areas.Item(2).Columns.Count) Then
  MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "Kutools for Excel"
  GoTo ExitSub
End If
Set xWordApp = CreateObject("Word.application")
xWordApp.Visible = True
Set xDoc = xWordApp.Documents.Open(xFileDlg.SelectedItems.Item(1))
For I = 1 To xRng.Areas.Item(1).Cells.Count
  With xDoc.Application.Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = xRng.Areas.Item(1).Cells.Item(I).Value
    .Replacement.Text = xRng.Areas.Item(2).Cells.Item(I).Value
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchByte = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
Next
ExitSub:
  Set xRng = Nothing
  Set xFileDlg = Nothing
  Set xWordApp = Nothing
  Set xDoc = Nothing
End Sub

3. 粘贴代码后,仍然在 Microsoft Visual Basic应用程序 窗口中,单击 工具 > 参考资料,请参见屏幕截图:

4。 在弹出的 参考– VBAProject 对话框中,选择 Microsoft Word 16.0对象库 从列表框中,看截图:

5。 点击 OK 按钮关闭对话框,现在,按 F5 键运行此代码,在弹出的浏览窗口中,选择要替换文本的 Word 文件,看截图:

6。 然后,点击 OK, 在以下对话框中,按 按Ctrl 键分别选择要使用的原始文本和新文本单元格,请参见屏幕截图:

7。 然后,单击 OK 按钮,现在,文本被找到并替换为您指定文档中的新文本,并且文件也正在打开,您应该保存它以保留更改。


使用 VBA 代码从 Excel 中查找和替换多个 Word 文档中的多个文本

在这里,我还创建了一个 VBA 代码,用于在多个 Word 文档中查找和替换多个文本,请这样做:

1. 打开包含两列要替换和替换的值的 Excel 文件,如下图所示,然后按 Alt + F11 同时打开 Microsoft Visual Basic应用程序 窗口。

2。 然后,点击 插页 > 模块,将以下 VBA 代码复制并粘贴到窗口中。

VBA 代码:在多个 Word 文件中查找和替换多个文本

Sub FindReplaceAcrossMultipleWordDocuments()
'Updateby ExtendOffice
Dim xWordApp As Word.Application
Dim xDoc As Word.Document
Dim xRng As Range
Dim I As Integer
Dim xFolderDlg As FileDialog
Dim xFSO As Scripting.FileSystemObject
Dim xFile As File
On Error GoTo ExitSub
Set xFolderDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xFolderDlg.Show <> -1 Then GoTo ExitSub
Set xRng = Application.InputBox("Please select the lists of find and replace texts (Press Ctrl key to select two same size ranges", "Kutools for Excel", , , , , , 8)
If xRng.Areas.Count <> 2 Then
  MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size", vbInformation + vbOKOnly, "Kutools for Excel"
  GoTo ExitSub
End If
If (xRng.Areas.Item(1).Rows.Count <> xRng.Areas.Item(2).Rows.Count) Or _
  (xRng.Areas.Item(1).Columns.Count <> xRng.Areas.Item(2).Columns.Count) Then
  MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "Kutools for Excel"
  GoTo ExitSub
End If
Set xFSO = New Scripting.FileSystemObject
Set xWordApp = CreateObject("Word.application")
xWordApp.Visible = True
For Each xFile In xFSO.GetFolder(xFolderDlg.SelectedItems(1)).Files
  If VBA.InStr(xFile.Type, "Microsoft Word") > 0 Then
    Set xDoc = xWordApp.Documents.Open(xFile.Path)
    For I = 1 To xRng.Areas.Item(1).Cells.Count
      With xDoc.Application.Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = xRng.Areas.Item(1).Cells.Item(I).Value
        .Replacement.Text = xRng.Areas.Item(2).Cells.Item(I).Value
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
      End With
      xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
    Next
    xDoc.Close wdSaveChanges
  End If
Next
xWordApp.Quit
MsgBox "The Find and Replace has been completed", vbInformation + vbOKOnly, "Kutools for Excel"
ExitSub:
  Set xRng = Nothing
  Set xFolderDlg = Nothing
  Set xWordApp = Nothing
  Set xDoc = Nothing
End Sub

3。 仍在 Microsoft Visual Basic应用程序 窗口中,单击 工具 > 参考资料参考– VBAProject 对话框中,选择 Microsoft Word 16.0对象库Microsoft脚本运行时 列表框中的选项,请参见屏幕截图:

4. 勾选两个选项后,点击 OK 关闭对话框,然后继续按 F5 执行此代码的关键,在开头 浏览 窗口,选择一个文件夹,其中包含要执行查找和替换的 Word 文档,看截图:

5。 点击 OK 按钮,在弹出的对话框中,按 按Ctrl 键分别选择要使用的原始文本和新文本列,请参见屏幕截图:

6。 最后点击 OK, 并将这些文件中的原始文本替换为新文本,完成后会弹出一个对话框,如下图所示:

7。 点击 OK 关闭对话框。 您可以转到文件来检查转换后的结果。


使用强大的功能查找和替换多个 Word 文档中的多个文本

本节,我将讨论如何从 Word 而不是 Excel 中批量查找和替换多个 Word 文档中的文本。 借助强大的工具——Kutools for Word,您可以快速查找和替换特定文本,并将其替换为主文件、页眉、页脚、注释等中的新文本,并根据需要突出显示结果。

1. 打开一个 Word 文件,然后单击 Kutools 加 > 批量查找和替换,请参见屏幕截图:

2。 在开 批量查找和替换 对话框,请执行以下操作:

  • 点击 地址 按钮添加要查找和替换文本的 Word 文件;
  • 在左侧窗格中,单击 添加行 从顶部功能区;
  • 在插入的字段中,将原始文本和新文本输入到 找到最适合您的地方更换 要查找和替换的单独列。 同样,您可以根据需要指定一种颜色来突出显示替换的文本。

3. 创建搜索条件后,单击 更换 按钮去 预览结果 选项卡以查看查找和替换结果。 看截图:

4。 然后,点击 关闭 按钮,会弹出提示框提醒您是否要保存该场景,点击 保存它,然后单击 没有 要忽略它,请参见屏幕截图:

Tips:此功能还可以帮助实现以下操作:
  • 查找和替换多个Word文档中的特殊字符;
  • 在多个 Word 文档中查找并替换具有特定格式的多个字符串;
  • 在多个 txt/htm/html 文件中查找和替换多个字符串。

单击以了解此功能的更多详细信息...

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (10)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
This works great, thank you! Is there a way to make the replacement text carry hyperlinks over? ie - if you have a hyperlinked replacement in the excel sheet, it is still hyperlinked in the Word doc?

Thanks!
This comment was minimized by the moderator on the site
Is there a way too modify this too find text and create hyperlink on the text from another column where i have the links already created? It worked correctly as a find and replace for me. Thanks
This comment was minimized by the moderator on the site
Hi,

I am wondering how this can be modified to also find and replace text in footnotes?

Thanks!
This comment was minimized by the moderator on the site
Hello, Nate,
If you want to find and replace the text in footnotes at the same time, maybe the Kutools for Word's Batch Find and Replace feature can help you.
You just need to check Main document and Footnotes from the Find in section, see below image:
https://www.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-find-replace-word.png
This comment was minimized by the moderator on the site
It doesn't work.

Compile error: User-defined type not defined
This comment was minimized by the moderator on the site
Hello, Param
The code works well.
Maybe, you didn't check Microsoft Word 16.0 Object Library from the References – VBAProject dialog box.
It means that you may miss the Step 3 and Step 4 of this article.
Please try again, if you still have any other problem, please comment here.
https://www.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-find-replace-word-file-excel.png
This comment was minimized by the moderator on the site
Sorry for the overdue reply. I have replied before, but my reply dissapeared somehow. You're right, the code does work well. But it replaced nothing when I tried it on a file with more than 80,000 lines.
This comment was minimized by the moderator on the site
Hello, Param
I have tested the code, it works well in my Word docuent which contains 140,000 lines.
Do you mind to upload your attachment here for testing?
Or you can apply our Kutools for Word's Batch Find and Replace feature, it can help you with ease.
Thank you!
This comment was minimized by the moderator on the site
Greetings,
the first code :
VBA code: Find and replace multiple texts in one Word file

thows error : compile error user defined type not defined
https://i.imgur.com/FZPBy4I.png
This comment was minimized by the moderator on the site
Hello, Erik
The code works well.
Maybe, you didn't check Microsoft Word 16.0 Object Library from the References – VBAProject dialog box.
It means that you may miss the Step 3 and Step 4 of this article.
Please try again, if you still have any other problem, please comment here.

https://www.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-find-replace-word-file-excel.png
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations