If you find yourself needing to replace identical content across several Word files, such as headers, footers, specific words, or numbers, doing so individually can be both time-consuming and cumbersome. However, there are more efficient methods available to accomplish this task.
One approach is to utilize VBA code to search and replace text across multiple Word documents simultaneously. Here’s how you can do it:
- Press Alt + F11 to open the Microsoft Visual Basic for Applications window.
- In the window, click Insert > Module, and then paste the provided VBA code into the Module window.
‘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
- Press the F5 key to execute the code.
- A Browse window will appear. Select the documents in which you want to find and replace text, then click OK.
- In the first dialog box that appears, enter the text you want to find across the documents and click OK.
- In the second dialog box, enter the text you want to replace it with and click OK.
-
Click OK in the subsequent Microsoft Word dialog box to complete the find and replace process.
With this method, you can quickly replace the specified text across multiple Word documents simultaneously.