1. 在 Excel 工作表中,创建一列包含您要查找和替换的文本,另一列包含用于替换的新内容,如下图所示。然后同时按下 Alt + F11 键,即可打开 Microsoft Visual Basic for Applications 窗口。
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 窗口中,单击工具> 引用,参见下图:
4. 在弹出的 引用 – VBAProject对话框中,从列表框中选择 Microsoft Word 16.0 Object Library,参见下图:
5. 单击确定按钮关闭对话框,然后按 F5 键运行此代码,在弹出的“浏览”窗口中选择您要替换文本的 Word 文件,参见下图:
我还编写了一段 VBA 代码,助您轻松在多个 Word 文档中批量查找并替换指定文本。请按以下步骤操作:
1. 打开包含“替换内容”和“替换为”两列值的 Excel 文件(如下图所示),然后同时按下 Alt + F11 键,即可打开 Microsoft Visual Basic for Applications 窗口。
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 Library 和 Microsoft Scripting Runtime 选项,参见下图:
4. 勾选这两个选项后,单击确定关闭对话框,然后按 F5 键执行此代码,在弹出的浏览窗口中选择包含您要执行查找和替换操作的 Word 文档的文件夹,参见下图: