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.

Kutools for Word: Add 100 New Advanced Features to Word 2003/2007/2010/2013.
Office Tab: Enable Tabbed Editing and Browsing in Office, Just Like Chrome, Firefox, IE 8/9/10.
Classic Menu for Office: Bring Old Look of Office 2003 Back to Office 2007, 2010 and 2013.

Using a VBA for searching and replacing same content across multiple documents at one time in Word:

Amazing! Using Tabs in Word like Firefox, Chrome, Internet Explore 10!

Learn moreFree download

Step 1: Press “Alt-F11” to open the Microsoft Visual Basic for Application window;

Step 2: Click Module on the Insert tab, copy and paste the following VBA code into the Module window;

Step 3: Then click Rundoc-remove-numeric-characters-1 button to apply the VBA.

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

Sub CommandButton1_Click()
Dim MyDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
On Error Resume Next
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
With MyDialog
.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
i = i - 1
End If
Application.ScreenUpdating = False
For j = 1 To i Step 1
Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True)
With Selection.Find
.Text = "search" 'Find What
.Replacement.Text = "find" '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"
Application.ScreenUpdating = True
End With
MsgBox "operation end, please view", vbInformation
End Sub

Note:you can change the Text = "search”, Replacement.Text = "find” to specify the content to be searched and the content to be applied for replacement.

arrow blue right bubble Kutools for Word

More than 100 Advanced Functions for Word 2003, 2007, 2010 and 2013


More Features  |  Free Download  |  Only $19.00 for hundreds of functions


+3#Bobette2013-11-14 08:20
Will it ask where the documents in question are located? Or do all the documents need to be open?
Reply | Reply with quote | Quote
0#Alison Pepper2013-12-31 15:42
Yes I have the same question as i think this would be useful but want to tell it to only look in certain folders.
Reply | Reply with quote | Quote
0#Patrick2014-03-12 15:29
When you run the macro, the "Open" window will open. Navigate to the folder that contains the files you need to make the change to, then click once on the first one, then, while holding down the "Ctrl" key, click on the other files also requiring the find/replace. If all files in the folder need the action, just press the "Ctrl" and "A" keys. Then click on "Open" or press the "Enter" key, and the macro will begin.

I should warn you, as I've performed this dozens of times at work: The macro will result in the changes being made only to the *body* of the Word document; headers, footers, and title text/alt-text will be excluded. If you have text in the aforementioned portions that needs replacing, you will have to perform a standard find/replace across individual documents.
Reply | Reply with quote | Quote
0#James R2014-03-19 11:47
Could this VBA technique be adapted to work for Publisher files? For example by changing *.docx to *.pub ?
Reply | Reply with quote | Quote
0#Jebidiah Springfield2014-03-24 17:07
Wow you are a saint for putting this macro together! I am listening to 'Epic Battle Music' on Songza because I am such an amatuer Macro-smith and it's the only way I can make it through the pain and agony of crafting programming spells.

Anyway, my point I am going to make is a spent a lot of time on Friday troubleshooting the part where you actually put in the text you want to find and putting in the text you want to replace - I did not quite understand this part in terms of where I put my "find" word text and "replace" word text:

Note:you can change the Text = "search”, Replacement.Text = "find” to specify the content to be searched and the content to be applied for replacement.

I read over this again today and I wish to respectfully clarify for other programming non-initiates - in the section of VBA where you have copied and pasted the original programming language, make sure you put an end ' after the what and a ' after the with (see example below) - in my example, I am looking for multiple documents to replace Pizza with Stromboli:

.Text = "Pizza" 'Find What'
.Replacement.Text = "Stromboli" 'Replace With'
Reply | Reply with quote | Quote
0#Greg Simonis2014-03-24 18:42
When I run it, it gets hung up and stops at:
Application.Run macroname:="NEWMACROS"
and when I step into it, it tells me I have a Compile error: Named argument not found.
Now what?
Reply | Reply with quote | Quote
0#Pia Wilson2014-06-04 09:18
What I am trying to replace is a web address on our standard documents. I tried just doing finding the web address within the hyperlink but its not working as although this text has changed the view when not toggled to see the field code is still showing the old address.

With Selection.Find
.Text = "HYPERLINK """" " 'Find What
.Replacement.Text = "HYPERLINK """" " 'Replace With
Reply | Reply with quote | Quote

Add comment

Security code