Skip to main content

How to search and replace across multiple files in word?

If you have several dozen word files which contain the same content (such as Header, footer, some special words or number), and you need to replace the same content across those documents in Word. How would it be easier for you to get it done quickly? Certainly, you can open those files one by one to replace the same content, but it will be time-consuming and troublesome. This tutorial will show you a tricky way to replace same content within multiple documents in Word at once.

Find and replace texts across multiple word documents at the same time with VBA code
Easily find and replace different texts across multiple documents at the same time with Kutools for Word


Find and replace texts across multiple word documents at the same time with VBA code

1. Press Alt + F11 to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click InsertModule, then copy  the following VBA code into the Module window.

VBA Code: Searching and replacing same content across multiple documents at one time

Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
Dim xFindStr As String
Dim xReplaceStr As String
Dim xDoc As Document
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With xFileDialog
    .Filters.Clear
    .Filters.Add "All WORD File ", "*.docx", 1
    .AllowMultiSelect = True
    i = 1
    If .Show = -1 Then
        For Each stiSelectedItem In .SelectedItems
            GetStr(i) = stiSelectedItem
            i = i + 1
        Next
        i = i - 1
    End If
    Application.ScreenUpdating = False
    xFindStr = InputBox("Find what:", "Kutools for Word", xFindStr)
    xReplaceStr = InputBox("Replace with:", "Kutools for Word", xReplaceStr)
    For j = 1 To i Step 1
        Set xDoc = Documents.Open(FileName:=GetStr(j), Visible:=True)
        Windows(GetStr(j)).Activate
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = xFindStr  'Find What
            .Replacement.Text = xReplaceStr  'Replace With
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        Application.Run macroname:="NEWMACROS"
        ActiveDocument.Save
        ActiveWindow.Close
    Next
    Application.ScreenUpdating = True
End With
MsgBox "Operation end, please view", vbInformation
End Sub

3. Press the F5 key to run the code.

4. In the opening Browse window, please find and select the documents you will find and replace text inside, and then click the OK button. See screenshot:

5. In the first Kutools for Word dialog box, enter the text you will find across documents into the Find what box, and then click the OK button.

6. In the second Kutools for Word dialog box, enter the text you will replace with, and click the OK button.

8. Click the OK button in the next Microsoft Word dialog box to finish the find and replace.

In this case, all words "Word" in selected documents are replaced with "Excel" at the same time.


Easily find and replace different texts across multiple documents at the same time with Kutools for Word

Here highly recommend the Batch Find and Replace feature of Kutools for Word. With this feature, you can easily find and replace different texts across multiple Word documents as the same time. Let's see how to apply this feature to find and replace texts across documents.

Kutools for Word : with more than 100 handy Word add-ins, free to try with no limitation in 60 days.

1. Please click Kutools Plus > Batch Find and Replace to enable the feature.

2. In the Batch Find and Replace dialog box, please configure as follows.

  • 2.1) Click the Add Row button under the Find and Replace tab;
  • 2.2) In the created row fields:
A. Enter the text you will find into the textbox in the Find column
B. Enter the text you will replace with into the textbox in the Replace column;
C. Specify a search type, where to find the text and a color to highlight the text as you need. In this case, I select Full word matching, Main document and a certain color from the Search Type, Find in and Highlight columns;
  • 2.3) Repeat the step 2.1 and 2.2 to add more texts you will find and replace;
  • 2.4) Click the  button in the File Type section to add the Word documents in which you will find and replace texts;
  • 2.5) Click the Replace or Find button. See screenshot:

Notes:
1. If you click the Find button, all finding results will be displayed under the Preview Result tab. After previewing the results, if you want to replace all texts, please click the Find and Replace tab. Otherwise, close the dialog.

2. If you click the Replace button, all specified texts will be replaced with new ones immediately, and the results will also be displayed under the Preview Result tab.

3. If you specify highlight colors in step 2, all replaced texts will be highlighted with specified colors, and you can easily find them at a glance in documents.

3. Close the Batch Find and Replace dialog box

For more information about this feature, please click here....

If you want to have a free trial of this utility, please go to free download the software first, and then go to apply the operation according above steps.

Best Office Productivity Tools

Kutools for Word - Elevate Your Word Experience with Over 100 Remarkable Features!

🤖 Kutools AI Assistant: Transform your writing with AI - Generate Content  /  Rewrite Text  /  Summarize Documents  /  Inquire for Information based on Document, all within Word

📘 Document Mastery: Split Pages  /  Merge Documents  /  Export Selection in Various Formats (PDF/TXT/DOC/HTML...)  /  Batch Convert to PDF  /  Export Pages as Images  /  Print Multiple Files at once...

Contents Editing: Batch Find and Replace across Multiple Files  /  Resize All Pictures  /  Transpose Table Rows and Columns  /  Convert Table to Text...

🧹 Effortless Clean: Sweap away Extra Spaces  /  Section Breaks  /  All Headers  /  Text Boxes  /  Hyperlinks  /  For more removing tools, head to our Remove Group...

Creative Inserts: Insert Thousand Separators  /  Check Boxes  /  Radio Buttons  /  QR Code  /  Barcode  /  Diagonal Line Table  /  Equation Caption  /  Image Caption  /  Table Caption  /  Multiple Pictures  /  Discover more in the Insert Group...

🔍 Precision Selections: Pinpoint specific pages  /  tables  /  shapes  /  heading paragraphs  /  Enhance navigation with more Select features...

Star Enhancements: Navigate swiftly to any location  /  auto-insert repetitive text  /  seamlessly toggle between document windows  /  11 Conversion Tools...

👉 Want to try these features? Kutools for Word offers a 60-day free trial, with no limitations! 🚀
Free Download     Read More     Buy Now
 
Comments (70)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
thanks for -How To Search And Replace Across Multiple Files In Word?

what about from out side (by folder) ?

thanks gain
This comment was minimized by the moderator on the site
This is a great explanation.  Is there a way to change settings, e.g. page size, simultaneously? 
This comment was minimized by the moderator on the site
After pressing F5, I get the following error" "Compile error: User-defined type not defined." Can someone help?
This comment was minimized by the moderator on the site
Hi Monique,Please check if the Microsoft Office Object Library is enabled as shown in the screenshot below.
This comment was minimized by the moderator on the site
This is SUCH a time saving solution. THANK YOU!
One thing that may make it even better, is adding support for subdirectories. (That is: Files in multiple subdirectories)
I am an absolute newbie in this, and don't know the syntax, the parameter, or where to add that.

Could somebody help?
This comment was minimized by the moderator on the site
I need to replace text in multiple Word files and have the replaced text highlighted. Is there a way to do this?
This comment was minimized by the moderator on the site
Hi Daphne,
Why not try the Batch Find and Replace feature of Kutools for Word? It can solve this issue with ease.
This comment was minimized by the moderator on the site
this was a lifesaver thank you so much
This comment was minimized by the moderator on the site
Is it possible to replace in the headers of the word doc?
This comment was minimized by the moderator on the site
Hi Tiago,
Please apply the below VBA code to include the content of headers and footers when finding and replacing texts in multiple documents at once.

Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
Dim xFindStr As String
Dim xReplaceStr As String
Dim xDoc As Document
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With xFileDialog
.Filters.Clear
.Filters.Add "All WORD File ", "*.docx", 1
.AllowMultiSelect = True
i = 1
If .Show = -1 Then
For Each stiSelectedItem In .SelectedItems
GetStr(i) = stiSelectedItem
i = i + 1
Next
i = i - 1
End If
Application.ScreenUpdating = False
xFindStr = InputBox("Find what:", "Kutools for Word", xFindStr)
xReplaceStr = InputBox("Replace with:", "Kutools for Word", xReplaceStr)
For j = 1 To i Step 1
Set xDoc = Documents.Open(FileName:=GetStr(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = xFindStr 'Find What
.Replacement.Text = xReplaceStr 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
If MsgBox("Do you want to replace texts in headers and footers too? ", vbYesNo, "Kutools for Word") = vbYes Then
ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
ActiveWindow.View.SplitSpecial = wdPanePrimaryHeader
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
End If
ActiveDocument.Save
ActiveWindow.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "Operation end, please view", vbInformation
End Sub
This comment was minimized by the moderator on the site
Is that possible to replace in the header of the word doc?
This comment was minimized by the moderator on the site
Thanks for sharing this information with us.
Word search and replace tool
This comment was minimized by the moderator on the site
Works perfectly but is there a similar macro to change text in the header! because this does not work with this version!
thanks
This comment was minimized by the moderator on the site
Hi,
Please apply the below VBA code to include the content of headers and footers when finding and replacing texts in multiple documents at once.

Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
Dim xFindStr As String
Dim xReplaceStr As String
Dim xDoc As Document
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With xFileDialog
.Filters.Clear
.Filters.Add "All WORD File ", "*.docx", 1
.AllowMultiSelect = True
i = 1
If .Show = -1 Then
For Each stiSelectedItem In .SelectedItems
GetStr(i) = stiSelectedItem
i = i + 1
Next
i = i - 1
End If
Application.ScreenUpdating = False
xFindStr = InputBox("Find what:", "Kutools for Word", xFindStr)
xReplaceStr = InputBox("Replace with:", "Kutools for Word", xReplaceStr)
For j = 1 To i Step 1
Set xDoc = Documents.Open(FileName:=GetStr(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = xFindStr 'Find What
.Replacement.Text = xReplaceStr 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
If MsgBox("Do you want to replace texts in headers and footers too? ", vbYesNo, "Kutools for Word") = vbYes Then
ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
ActiveWindow.View.SplitSpecial = wdPanePrimaryHeader
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
End If
ActiveDocument.Save
ActiveWindow.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "Operation end, please view", vbInformation
End Sub
This comment was minimized by the moderator on the site
Oh yeah! Thank you sooooo much! This is a life-saver!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations