跳至主要内容

如何使用Excel在Word中查找和替换多个文本?

Author: Xiaoyang Last Modified: 2025-05-08

在Microsoft Word中,查找和替换功能是一种快速搜索和替换特定文本的高效方法。然而,当您需要替换多个不同的术语时,手动输入每个术语可能会非常耗时。

为了简化此过程,您可以使用Excel创建一个要查找和替换的文本列表,然后通过简单的VBA代码自动完成任务。本教程将指导您如何使用Excel和VBA在一个Word文档中查找和替换多个文本。此外,我还将向您展示如何将其扩展到多个文档,并介绍Kutools的一项强大功能,用于批量文本替换。

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

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

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


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

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

1. 在Excel工作表中,创建一列包含您想要查找和替换的文本,另一列包含要替换成的文本,如下图所示。然后同时按下Alt+F11键打开Microsoft Visual Basic for Applications窗口。

A screenshot showing an Excel worksheet with columns containing texts to find and replace

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 for Applications窗口中,点击工具 > 引用,见截图:

A screenshot showing the Microsoft Visual Basic for Applications window with the Tools menu expanded

4. 在弹出的“引用 - VBAProject”对话框中,从列表框中选择Microsoft Word 16.0 Object Library,见截图:

A screenshot showing the References dialog box in the Microsoft Visual Basic for Applications window with the Microsoft Word 16.0 Object Library selected

5. 单击确定按钮关闭对话框,现在,按F5键运行此代码,在弹出的浏览窗口中,选择您要替换文本的Word文件,见截图:

A screenshot showing the Browse window for selecting a Word file

6. 然后,单击确定,在接下来的对话框中,按Ctrl键分别选择原始文本和新文本单元格,见截图:

A screenshot showing the selection of two ranges in Excel for find and replace text

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


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

我还创建了一个VBA代码来帮助您在多个Word文档中查找和替换多个文本。请按照以下步骤操作:

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

A screenshot showing an Excel worksheet with columns containing texts to find and replace

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 for Applications窗口中,点击工具 > 引用,在“引用 - VBAProject”对话框中,从列表框中选择Microsoft Word 16.0 Object LibraryMicrosoft Scripting Runtime选项,见截图:

A screenshot showing the References dialog box with Microsoft Word 16.0 Object Library and Microsoft Scripting Runtime selected

4. 勾选这两个选项后,单击确定关闭对话框,然后继续按F5键执行此代码,在打开的 浏览窗口中,选择包含您要进行查找和替换操作的Word文档的文件夹,见截图:

A screenshot showing the folder selection dialog for choosing a folder containing Word documents for find and replace

5. 单击确定按钮,在弹出的对话框中,按Ctrl键分别选择您要使用的原始文本和新文本列,见截图:

A screenshot showing the selection of original and new text columns in Excel for find and replace

6. 最后,单击确定,这些文件中的原始文本已被替换为新的文本,完成后,会弹出一个对话框,如下图所示:

A screenshot showing the confirmation dialog box after completing the find and replace process across multiple Word documents

7. 单击确定关闭对话框。然后您可以前往文件检查转换结果。


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

在本节中,我将解释如何使用Word本身而不是Excel,通过强大的Kutools for Word批量查找和替换多个Word文档中的文本。您可以高效地搜索特定文本并将其替换为新文本,包括正文、页眉、页脚、批注等,同时还可以根据需要高亮显示结果。

Kutools for Word内置 AI 🤖,提供超过 100 种实用功能,助您简化任务。

1. 打开一个Word文件,然后点击Kutools Plus > 批量查找和替换,见截图:

A screenshot showing the Kutools for Word Batch Find and Replace feature in the Ribbon

2. 在打开的“批量查找和替换”对话框中,请执行以下操作:

  • 点击添加按钮,添加您要查找和替换文本的Word文件;
  • 在左侧窗格中,点击顶部功能区中的添加行
  • 在插入的字段中,分别在查找替换列中输入您要查找和替换的原始文本和新文本。同样,您可以根据需要指定一种颜色来高亮显示替换后的文本。

A screenshot showing the Batch Find and Replace dialog box with the option to add Word files

3. 创建搜索条件后,点击替换按钮进入预览结果选项卡查看查找和替换结果。见截图:

A screenshot showing the Preview Result tab after performing find and replace

4. 然后,单击关闭按钮,会弹出一个提示框询问您是否要保存此方案,单击保存,单击忽略,见截图:

A screenshot showing the prompt asking if the user wants to save the batch find and replace scenario in Kutools for Word

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

点击了解更多关于此功能的详细信息…

借助 AI 增强的 Kutools for Word,在更短时间内完成更多工作

Kutools for Word 不仅仅是一套工具——它是一个旨在提升您生产力的智能解决方案。通过人工智能驱动的功能和最核心的特性,Kutools 帮助您在更短的时间内完成更多任务:

  • 即时总结、润色、撰写和翻译内容。
  • 在您写作时,实时校正文本,并提供语法、标点和样式建议。
  • 在保持布局、样式和结构不变的情况下,重新表达和翻译内容。
  • 轻松将您的内容翻译成 40 多种语言,扩大全球影响力。
  • 根据当前文档内容,获得即时帮助和智能见解。
  • 询问如何完成某项任务(例如清除分节符),AI 将为您提供指南或直接为您完成操作。
  • 快速编辑敏感或机密信息,确保完全隐私。
  • 所有工具均可无缝集成到 Word 中,随时可用。
  • 轻松创建、优化、翻译、总结和保护文档。
  • 在实时写作过程中改进语法、清晰度和语气。
  • 重新表达和翻译内容,且不改变布局或格式。
  • 询问如何完成某项任务(例如清除分节符),AI 将为您提供指南或直接为您完成操作。
  • 所有工具均可无缝集成到 Word 中,随时可用。
了解更多关于 Kutools for Word 的信息 立即下载
Kutools for Word features

最佳办公效率工具

🤖 Kutools AI 助手:基于智能执行生成代码创建自定义公式分析数据并生成图表调用 Kutools 函数等功能,彻底改变数据分析方式…
热门功能查找、高亮或标记重复项 | 删除空行 | 合并不丢失数据的列或单元格 | 四舍五入 ...
高级 LOOKUP多条件 VLookup | 多值 VLookup | 多表查找 | 模糊查找 ....
高级下拉列表快速创建下拉列表 | 从属下拉列表 | 多选下拉列表 ....
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域和列 ...
精选功能网格聚焦 | 设计视图 | 增强编辑栏 | 工作簿与工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/斜体/删除线...)...
排名前 15 的工具集12 种文本 工具添加文本删除特定字符等) | 50 多 种图表 类型甘特图等) | 40 多种实用 公式基于生日计算年龄等) | 19 种插入 工具插入二维码根据路径插入图片等) | 12 种转换 工具小写金额转大写汇率转换等) | 7 种合并与分割 工具高级合并行分割单元格等) | 还有更多...

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


Office Tab 将标签式界面引入 Office,让您的工作更加轻松

  • 在 Word、Excel、PowerPoint 中启用标签式编辑和阅读
  • 在同一窗口的新标签页中打开和创建多个文档,而不是在新窗口中进行操作。
  • 将您的生产力提升 50%,每天为您减少数百次鼠标点击!