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:

Hot
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.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 and 2013

shot-kutools-700-225-kte

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

Comments  

+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:

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'
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.

EG
With Selection.Find
.Text = "HYPERLINK ""http://www.testsite.co.uk"" " 'Find What
.Replacement.Text = "HYPERLINK ""http://www.testtwo.co.uk"" " 'Replace With
Reply | Reply with quote | Quote
0#Muralisk2014-08-07 06:04
Worked like a charm and it did save a lot of time and effort for me. Thanks.
Reply | Reply with quote | Quote
0#Greg Simonis2014-08-08 15:22
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?
Reply | Reply with quote | Quote
0#Pia Wilson2014-08-11 07:22
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:=GetStr(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.
Reply | Reply with quote | Quote
0#Pia Wilson2014-08-11 07:22
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
.MatchSoundsLike = 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.Hyperlinks(i).Address) = "http://www.google.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).TextToDisplay = "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
Reply | Reply with quote | Quote

Add comment


Security code
Refresh