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/2016.
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, 2013 and 2016.

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

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

Learn more Free 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 Run doc-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.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
For j = 1 To i Step 1
Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
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"
ActiveDocument.Save
ActiveWindow.Close
Next
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, 2013 and 2016

shot-kutools-700-225-kte

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

Comments  

Permalink +1 Bobette
Will it ask where the documents in question are located? Or do all the documents need to be open?
2013-11-14 08:20 Reply Reply with quote Quote
Permalink 0 PatrickRsGhost
When you run it, by default it will open up the "Open File" dialogue window to "My Documents". Navigate to the folder that contains the Word files you wish to make the changes to. You can press "Ctrl" + "A" to select all files, or hold down the "Ctrl" key while selecting certain files. Click on "Open" or press "Enter" and watch the magic happen.
2014-10-30 10:21 Reply Reply with quote Quote
Permalink 0 Alison Pepper
Yes I have the same question as i think this would be useful but want to tell it to only look in certain folders.
2013-12-31 15:42 Reply Reply with quote Quote
Permalink +1 Patrick
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.
2014-03-12 15:29 Reply Reply with quote Quote
Permalink 0 James R
Could this VBA technique be adapted to work for Publisher files? For example by changing *.docx to *.pub ?
2014-03-19 11:47 Reply Reply with quote Quote
Permalink +2 Jebidiah Springfield
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:

Quote:
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'
2014-03-24 17:07 Reply Reply with quote Quote
Permalink 0 Greg Simonis
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?
2014-03-24 18:42 Reply Reply with quote Quote
Permalink 0 Pia Wilson
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.

EG
With Selection.Find
.Text = "HYPERLINK ""http://www.te stsite.co.uk"" " 'Find What
.Replacement.Text = "HYPERLINK ""http://www.te sttwo.co.uk"" " 'Replace With
2014-06-04 09:18 Reply Reply with quote Quote
Permalink 0 Muralisk
Worked like a charm and it did save a lot of time and effort for me. Thanks.
2014-08-07 06:04 Reply Reply with quote Quote
Permalink 0 Greg Simonis
I still cannot get this to work on my PC running Windows 7.
When I run it, I get an error msg at the line:
-Windows(GetStr(j)).Activate
The error msg. is:
-Compile error:
-Sub or Function not defined
Any idea why? And how to fix it?
2014-08-08 15:22 Reply Reply with quote Quote
Permalink 0 Pia Wilson
This is what finally worked for me. I hope it helps. I added this macro to a word file. It will run the replace any documents selected (you can select as many as you like) when the file dialoge is open. It wont work on sub folders so I did each folder in the file structure individually but it worked.It may not be the best fix but it works at least.


Private 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.Clear
'~~> Change this file extention to the relevant doc type.
.Filters.Add "All WORD File ", "*.doc", 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
For j = 1 To i Step 1
Set doc = Documents.Open( FileName:=GetSt r(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
'Section then either one or the other of sections of code to follow.
2014-08-11 07:22 Reply Reply with quote Quote
Permalink 0 Pia Wilson
You then add one of the following two sections depending on if it is test or hyperlink to be replaced

'~~> This section if uncommented will replace text - ONLY ONE OF TWO SECTIONS CAN BE UNCOMMENTED AT ONCE


With Selection.Find
.Text = "www.google.co. uk" 'Find What
.Replacement.Text = "www.google.com " 'Replace With
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLik e = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Application.Run macroname:="NEWMACROS"
ActiveDocument.Save
ActiveDocument.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "operation end, please view", vbInformation


'~~> This section if uncommented will replace hyperlinks
'For i = 1 To doc.Hyperlinks.Count
' 'If the hyperlink matches.
' If LCase(doc.Hyper links(i).Addres s) = "http://www.goo gle.co.uk/" Then
' 'Change the links address.
' doc.Hyperlinks( i).Address = "https://www.google.com"
' 'Change the links display text if desired.
' doc.Hyperlinks( i).TextToDispla y = "www.google.com "
' End If
' Next
'Application.Run macroname:="NEWMACROS"
'ActiveDocument.Save
'ActiveDocument.Close
'Next
'Application.ScreenUpdating = True
'End With
'MsgBox "operation end, please view", vbInformation



End Sub
2014-08-11 07:22 Reply Reply with quote Quote
Permalink 0 segarn
Hi Pia,
I am trying to run your script for replacing hyperlinks but am getting errors. Any chance you can post the script for replacing hyperlinks all in 1 piece? I must be messing something up when trying to patch your code together.
2014-09-02 13:10 Reply Reply with quote Quote
Permalink 0 Pia
It only runs across selected files and files must be in one folder only. This is run as a macro in a word doc. It is run from clicking a button. It only updates word doc's Hope this helps.


Private 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.Clear
'~~> Change this file extention to the relevant doc type.
.Filters.Add "All WORD File ", "*.doc", 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
For j = 1 To i Step 1
Set doc = Documents.Open( FileName:=GetSt r(j), Visible:=True)
Windows(GetStr(j)).Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

For i = 1 To doc.Hyperlinks. Count
'If the hyperlink matches.
If LCase(doc.Hyper links(i).Addres s) = "http://www.yah oo.co.uk/" Then
'Change the links address.
doc.Hyperlinks( i).Address = "https://www.go ogle.co.uk"
'Change the links display text if desired.
doc.Hyperlinks( i).TextToDispla y = "www.google.co. uk"
End If
Next
Application.Run macroname:="NEWMACROS"
ActiveDocument.Save
ActiveDocument.Close
Next
Application.ScreenUpdating = True
End With
MsgBox "operation end, please view", vbInformation
End Sub
2014-09-05 13:30 Reply Reply with quote Quote
Permalink 0 Dani
This worked perfectly and saved me so much time. Thank you!
2014-09-23 17:38 Reply Reply with quote Quote
Permalink +1 Judy
Hello,
This worked great for the word in the body of the document but did not replace the same word in the header. Any ideas.

Thanks
2014-10-29 14:24 Reply Reply with quote Quote
Permalink 0 Nilesh
I want to change the format of the replaces text. i.e. "Pizza" has to be replaced with "Burger".
How can it be achieved?
2014-11-05 06:18 Reply Reply with quote Quote
Permalink 0 WORDZ
Big THX 4 the code... but also have that prblm with the headers... no change

Thanks in advance
2015-04-02 12:27 Reply Reply with quote Quote
Permalink 0 sathena99
Thanks for posting this! I've run a couple tests and gotten it to work for me; however, it's not working on text in headers and footers for some reason. Is there a fix for this?
2015-04-14 15:37 Reply Reply with quote Quote
Permalink 0 Pia
I am sorry but I have no idea re the headers or the format. It was not something I needed to include in my code. I had never used this before myself and the code I posted was taken from the original code above and then tweeked using code found in other searches until it worked. I am sorry I cant be of more help.
2015-04-15 07:22 Reply Reply with quote Quote
Permalink 0 Moh
I've been testing it for the last few minutes and it's really a wonder, thank you!

I need to perform several 'find and replace' routines for several files. Dozens of routines for dozens of files.

Is there a way to 'save' the module so I don't need to paste it again on the VBA screen every time I need to perform a new routine?
2015-04-24 14:13 Reply Reply with quote Quote
Permalink 0 Mario
Is it possible to tweak this macro to perform multiple 'find and replace' stances at once? There are several words and phareses I need to replace and as of now I need to reinsert the VBA code for every instance.

I'm a noob regarding those things
2015-04-24 18:27 Reply Reply with quote Quote
Permalink 0 Milo
This code has saved so much time for me... so, thanks!

One thing though - is there any way to increase the number of words that the macro finds/replaces? I need to find/replace multiple paragraphs at once and the macro seems to have a limit of approximately 200 characters.
2015-07-09 10:59 Reply Reply with quote Quote
Permalink 0 Merlin409
I ran across this code while searching with DuckDuckGo. I modified it to suit my needs which were to other requests such as handling multiple search/replace in the header and multiple search/replace in the body. I also modified it handle the older .DOC format and the new .DOCX format. The modified code is pasted below. Feel free to use and improve!

It is being sent in multiple parts because comments to this site can only be 2000 characters or less.
--------------- --------------- --------------- ---------------
2015-08-19 19:42 Reply Reply with quote Quote
Permalink +1 Merlin409
Sub Search_and_Replace()

' 500 files is the maximum applying this code
Dim MyDialog As FileDialog, GetStr(1 To 500) As String

On Error Resume Next

Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)

' --------------- --------------- --------------- ------------------
' *.doc? allows processing of *.doc and *.docx files.
' --------------- --------------- --------------- ------------------
With MyDialog
.Filters.Clear
.Filters.Add "All WORD File ", "*.doc?", 1
.AllowMultiSele ct = 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.Scr eenUpdating = False

For j = 1 To i Step 1
Set Doc = Documents.Open( FileName:=GetSt r(j), Visible:=True)

' --------------- --------------- --------------- ----------
' Beginning Header Updates
' --------------- --------------- --------------- ----------
Windows(GetStr( j)).Activate
Selection.Find. ClearFormatting
Selection.Find. Replacement.Cle arFormatting

If ActiveWindow.Vi ew.SplitSpecial wdPaneNone Then
ActiveWindow.Pa nes(2).Close
End If

If ActiveWindow.Ac tivePane.View.T ype = wdNormalView Or ActiveWindow. _
ActivePane.View .Type = wdOutlineView Then
ActiveWindow.Ac tivePane.View.T ype = wdPrintView
End If

ActiveWindow.Ac tivePane.View.S eekView = wdSeekCurrentPa geHeader
Selection.Find. ClearFormatting
Selection.Find. Replacement.Cle arFormatting
2015-08-19 19:43 Reply Reply with quote Quote
Permalink +1 Merlin409
' --- Beging header text replacemnt 1 ---
With Selection.Find
.Text = "Header String 1" ' Find What
.Replacement.Te xt = "HReplacement 1" ' Replace With
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLik e = False
.MatchAllWordFo rms = False

If .Found = True Then
ChangeMade = True
End If

End With
Selection.Find. Execute Replace:=wdRepl aceAll
' --- End header text replacemnt 1 ---
2015-08-19 19:45 Reply Reply with quote Quote
Permalink +1 Merlin409
' --- Beging header text replacemnt 2 ---
With Selection.Find
.Text = "Header String 2" ' Find What
.Replacement.Te xt = "HReplacement 2" ' Replace With
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLik e = False
.MatchAllWordFo rms = False

If .Found = True Then
ChangeMade = True
End If

End With
Selection.Find. Execute Replace:=wdRepl aceAll
' --- Beging header text replacemnt 2 ---

ActiveWindow.Ac tivePane.View.S eekView = wdSeekMainDocum ent
' --------------- --------------- --------------- ---------
' End of Header Updates
' --------------- --------------- --------------- ---------


' --------------- --------------- --------------- ----------
' Beginning of Body Updates
' --------------- --------------- --------------- ----------
Windows(GetStr( j)).Activate
Selection.Find. ClearFormatting
Selection.Find.Replacement.ClearFormatting

' ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~
' Delete additional body replacement blocks if
' not needed or copy/paste additional blocks if
‘ required.
' ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~
2015-08-19 19:45 Reply Reply with quote Quote
Permalink +1 Merlin409
' --- Beging body text replacemnt 1 ---
With Selection.Find
.Text = "Body text 1" ' Find What
.Replacement.Te xt = "BT1" ' Replace With
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLik e = False
.MatchAllWordFo rms = False
End With
Selection.Find. Execute Replace:=wdRepl aceAll
' --- Beging body text replacemnt 1 ---

' --- Beging body text replacemnt 2 ---
With Selection.Find
.Text = "membership" ' Find What
.Replacement.Te xt = "team members" ' Replace With
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLik e = False
.MatchAllWordFo rms = False
End With
Selection.Find. Execute Replace:=wdRepl aceAll
' --- End body text replacemnt 2 ---
2015-08-19 19:47 Reply Reply with quote Quote
Permalink +1 Merlin4099
' --- Beging body text replacemnt 3 ---
With Selection.Find
.Text = "leadership" ' Find What
.Replacement.Te xt = "supervision" ' Replace With
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLik e = False
.MatchAllWordFo rms = False
End With
Selection.Find. Execute Replace:=wdRepl aceAll
' --- End body text replacemnt 3 ---

Application.Run macroname:="NEW MACROS"
ActiveDocument. Save
ActiveWindow.Cl ose
' --------------- --------------- --------------- ----------
' End of Body Updates
' --------------- --------------- --------------- ----------
Next

Application.Scr eenUpdating = True

End With

MsgBox "operation end, please view", vbInformation

End Sub
2015-08-19 19:47 Reply Reply with quote Quote
Permalink 0 Ravini
To Merlin4099
Wonderful code - thank you!! worked perfectly in executing what I needed.
2017-01-18 16:06 Reply Reply with quote Quote
Permalink 0 Exis,LLC
You can use a program such as www.officefindreplace.com to perform the same operation on multiple documents, or multiple operations on the same document. www.officefindreplace.com
2016-01-20 01:02 Reply Reply with quote Quote
Permalink 0 fatma
this was sooo helpful, thank you very much! I have just changed the contents in 90 word files. This is amazing! Just one more tip: at the beginning close all open word documents but one of your file to be change.
2016-02-02 11:41 Reply Reply with quote Quote
Permalink 0 Jelena
Is it possible to change code and search and replace photo in multiply files?
2016-02-25 15:16 Reply Reply with quote Quote
Permalink 0 Tali1953
To Merlin4099
You have just saved my life (... time...) with you code !
THANK YOU !!!
2016-04-10 15:53 Reply Reply with quote Quote
Permalink 0 Nick
I am trying to add an entire section to a document that has three lines, bolding, and bullet points. Is there a way to keep that formatting when running the macro?
2016-06-07 19:06 Reply Reply with quote Quote
Permalink 0 Exis
You can always try a 3rd party tool to do this which makes it very easy: www.officefindreplace.com
2016-07-11 20:51 Reply Reply with quote Quote
Permalink 0 Tracy
Just brilliant, saved me lots of time, thank you
2016-09-06 08:47 Reply Reply with quote Quote
Permalink 0 Mike
This macro was amazing..is there any code to update multiple excel files,spss files.also is there is any macro to update the header and footer on multiple word documents .

Any help is appreciated .thanks !
2016-09-21 15:05 Reply Reply with quote Quote
Permalink 0 Terence
Out of interest what does the line: Application.Run macroname:="NEW MACROS" do?
The rest of the code makes sense to me (used to VBA in Excel) but I can't figure out why you need to execute another piece of code here.
Thanks for the great tool though.
2017-01-25 05:26 Reply Reply with quote Quote
Permalink 0 Paul Oz
Hi all,
I am new to the whole VBA thing and I Love love love this code!
I have 5000+ docs to review and if this works it will save heaps of time!
I have the code above running however i can see that it does not cater for replacing text in footers, Only headers and the body of the doc.
Can anyone assist in extending this to the entire document.
P.S. not sure if this is relevant but the list of words that i need to replace is the same for headers, footers and the body...
Any help would really be appreciated!
2017-03-27 01:05 Reply Reply with quote Quote

Add comment


Security code
Refresh