How to find and replace multiple texts in Word using Excel?
AuthorXiaoyang•Last modified
In Microsoft Word, the Find and Replace feature is an efficient way to quickly search for and replace specific text. However, when you need to replace multiple different terms, manually entering each one can be time-consuming.
To streamline this process, you can use Excel to create a list of the texts to find and replace, and then automate the task with a simple VBA code. This tutorial will guide you through using Excel and VBA to find and replace multiple texts within a single Word document. Additionally, I’ll show you how to extend this to multiple documents and introduce a powerful Kutools feature for batch text replacement.
Find and replace multiple texts in one Word document from Excel with VBA code
If you want to find and replace some texts in only one Word file, the following VBA code can do you a favor.
1. In Excel worksheet, create a column containing the texts you want to find and replace, and another column with texts to replace with as below screenshot shown. And then press Alt+ F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.
2. Then, click Insert > Module, copy and paste the below VBA code into the window.
VBA code: Find and replace multiple texts in one Word file
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. After pasting the code, still in the Microsoft Visual Basic for Applications window, click Tools > References, see screenshot:
4. In the popped-out References – VBAProject dialog box, select the Microsoft Word 16.0 Object Library from the list box, see screenshot:
5. Click OK button to close the dialog box, and now, press F5 key to run this code, in the pop up Browse window, select the Word File you want to replace the texts, see screenshot:
6. Then, click OK, in the following dialog box, press Ctrl key to select the original text and new text cells separately that you want to use, see screenshot:
7. And then, click OK button, now, the texts are found and replaced with the new texts in your specified document, and the file is opening as well, you should save it to keep the changes.
Find and replace multiple texts in multiple Word documents from Excel with VBA code
I've also created a VBA code to help you find and replace multiple texts across multiple Word documents. Follow these steps:
1. Open the Excel file that contains two columns of values to replace and replace with as below screenshot shown, and then press Alt+ F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.
2. Then, click Insert > Module, copy and paste the below VBA code into the window.
VBA code: Find and replace multiple texts in multiple Word files
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. Still in the Microsoft Visual Basic for Applications window, click Tools > References, in the References – VBAProject dialog box, select the Microsoft Word 16.0 Object Library and Microsoft Scripting Runtime options from the list box, see screenshot:
4. After checking the two options, and click OK to close the dialog box, and then, go on pressing the F5 key to execute this code, in the opening Browse window, choose a folder contains the Word documents that you want to perform the find and replace, see screenshot:
5. Click OK button, in the popped-out dialog box, press Ctrl key to select the original text and new text columns separately that you want to use, see screenshot:
6. Finally, click OK, and the original texts are replaced with the new ones across theses files, after completing, a dialog box will pop out as below screenshot shown:
7. Click OK to close the dialog. And you can go to the files to check the converted results.
Find and replace multiple texts in multiple Word documents with a powerful feature
In this section, I'll explain how to batch find and replace text across multiple Word documents using Word itself, rather than Excel. By using the powerful Kutools for Word, you can efficiently search for specific texts and replace them with new ones in the main body, headers, footers, comments, and more, while also highlighting the results as needed.
Kutools for Word, equipped with AI 🤖, offers over 100 handy features to simplify your tasks.
1. Open one Word file, and then click Kutools Plus > Batch Find and Replace, see screenshot:
2. In the opened Batch Find and Replace dialog box, please do the following operations:
Click Add button to add the Word files where you want to find and replace texts;
In the left pane, click Add row from the top ribbon;
In the inserted field, enter the original text and new text into the Find and Replace columns separately that you want to find and replace. As well, you can specify a color for highlighting the replaced texts as you need.
3. After creating the search criteria, click Replace button to go to the Preview Result tab to view the find and replace results. See screenshot:
4. Then, click Close button, and a prompt box is popped out to remind you if you want to save this scenario, click Yes to save it, and click No to ignore it, see screenshot:
Tips: This feature also can help to achieve the following operations:
Find and replace special characters in multiple Word documents;
Find and replace multiple strings with specific formatting in multiple Word documents;
Find and replace multiple strings in multiple txt/htm/html files.
Do More in Less Time with AI-Enhanced Kutools for Word
Kutools for Word isn't just a set of tools - it's a smart solution designed to boost your productivity. With AI-driven capabilities and the most essential features, Kutools helps you accomplish more in less time:
Summarize, rewrite, compose, and translate content instantly.
Proofread text in real-time with grammar, punctuation, and style suggestions as you write.
Rephrase and translate content while keeping layout, style, and structure untouched.
Translate your content into over 40 languages with ease, expanding your reach globally.
Receive instant help and intelligent insights based on your current document content.
Ask how to complete a task - like removing section breaks - and the AI will guide you or do it for you.
Redact sensitive or confidential information in seconds to ensure complete privacy.
All tools work seamlessly inside Word, always within reach.
Create, refine, translate, summarize and secure documents effortlessly.
Improve grammar, clarity, and tone as you write in real time.
Rephrase and translate content with no layout or formatting changes.
Ask how to complete a task - like removing section breaks - and the AI will guide you or do it for you.
All tools work seamlessly inside Word, always within reach.
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...