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.
Recommended Productivity Tools for Word
1. Press Alt + F11 to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, click Insert > Module, 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.
Recommended Word Productivity Tools
- Complicated and repeated operations can be done one-time processing in seconds.
- Insert multiple images across folders into Word document at once.
- Merge and combine multiple Word files across folders into one with your desired order.
- Split the current document into separate documents according to heading, section break or other criteria.
- Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
You are guest
or post as a guest, but your post won't be published automatically.
- To post as a guest, your comment is unpublished.· 6 years agoCould this VBA technique be adapted to work for Publisher files? For example by changing *.docx to *.pub ?
- To post as a guest, your comment is unpublished.· 6 years agoWhen 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.
- To post as a guest, your comment is unpublished.· 6 years agoYes I have the same question as i think this would be useful but want to tell it to only look in certain folders.
- To post as a guest, your comment is unpublished.· 6 years agoWill it ask where the documents in question are located? Or do all the documents need to be open?
- To post as a guest, your comment is unpublished.· 5 years agoWhen 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.