Manually finding and replacing text in a long document is slow and error prone. Word’s built-in Find and Replace dialog works for simple searches but cannot handle conditional logic or batch processing across multiple documents. VBA macros let you automate complex find and replace tasks with precision. This article explains how to write and run VBA code to find and replace text in Word, covering basic replacements, wildcard searches, and formatting changes.
Key Takeaways: Automating Find and Replace With VBA in Word
- Selection.Find.Execute method: Performs a find operation on the current selection or range.
- Range.Find object properties: Set Text, Replacement.Text, Forward, Wrap, and Format to control the search.
- wdReplaceAll constant: Replaces all occurrences in one call without looping.
How VBA Find and Replace Works in Word
VBA uses the Find object attached to a Range object. The Range can be the entire document, a selection, a paragraph, or a custom range. You set properties on the Find object to define what to search for and how to replace it. The key method is Execute, which runs the search and optionally performs the replacement.
The Find object supports the same options as the Find and Replace dialog. You can search for text, formatting, special characters (like paragraph marks), and use wildcards. The Replacement object inside Find holds the replacement text and formatting. Setting Replacement.Text defines what will replace the found text. The Wrap property controls what happens when the search reaches the end of the range.
To replace all occurrences in the document, you call Execute with the Replace parameter set to wdReplaceAll. This runs the replacement in one pass without looping. Looping is only needed when you need to inspect each match before deciding whether to replace it.
Prerequisites
You need access to the VBA editor. Press Alt+F11 to open it. The document must be a macro-enabled file (.docm) or you must save it as .docm after adding the macro. Enable macros in the Trust Center if prompted. You do not need advanced programming experience, but you should be comfortable copying and pasting code into the editor.
Steps to Write a VBA Macro for Find and Replace
- Open the VBA Editor
Press Alt+F11 in Word. The VBA editor window opens. In the Project Explorer, find your document name under Normal or Project. - Insert a new module
Click Insert > Module from the menu. A blank code window appears. This is where you will paste the macro code. - Paste the macro code
Copy the code below and paste it into the module window. The code replaces every instance of “old text” with “new text” in the entire document.
Sub FindAndReplaceBasic()
With ActiveDocument.Range.Find
.Text = "old text"
.Replacement.Text = "new text"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
- Run the macro
Press F5 while the cursor is inside the macro code. Alternatively, close the editor and press Alt+F8 in Word. Select FindAndReplaceBasic from the list and click Run. - Verify the result
Scroll through the document to confirm that “old text” has been replaced with “new text”. If the replacement did not work, check that the text matches exactly including spaces and punctuation.
Using Wildcards for Pattern Matching
Set .MatchWildcards = True to use wildcard patterns. For example, to replace any sequence of two digits followed by a space with “XX “, use this code:
Sub FindAndReplaceWildcards()
With ActiveDocument.Range.Find
.Text = "[0-9]{2} "
.Replacement.Text = "XX "
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
Replacing Formatting
To replace text and apply formatting, set the Replacement.Font or Replacement.ParagraphFormat properties. The example below replaces all instances of “urgent” with “URGENT” in bold red text:
Sub FindAndReplaceFormatting()
With ActiveDocument.Range.Find
.Text = "urgent"
.Replacement.Text = "URGENT"
.Replacement.Font.Bold = True
.Replacement.Font.Color = wdColorRed
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Execute Replace:=wdReplaceAll
End With
End Sub
Common Mistakes and Limitations When Using VBA for Find and Replace
VBA Macro Runs but Nothing Changes
The most common cause is a mismatch in the search text. Check for extra spaces, non-breaking spaces, or hidden characters. Copy the exact text from the document into the .Text property. Also verify that .MatchCase and .MatchWholeWord are set correctly. If the text contains special characters like paragraph marks, use the VBA constant ^p instead of the actual character.
Macro Replaces Text in Headers or Footers
The code above only acts on the main document body. To replace text in headers and footers, you must loop through each section’s headers and footers. Use a For Each loop over ActiveDocument.Sections and access the Headers and Footers collections. Each header range has its own Find object.
VBA Macro Is Too Slow on Large Documents
Using wdReplaceAll is faster than looping with Find.Execute repeatedly. If you must loop, disable screen updating with Application.ScreenUpdating = False at the start of the macro and set it back to True at the end. Also disable Application.DisplayAlerts to suppress pop-ups.
Macro Does Not Work After Saving as .docx
Word strips VBA code from .docx files. Save the document as a macro-enabled document (.docm). If you open a .docm file and macros are disabled, enable them via File > Options > Trust Center > Trust Center Settings > Macro Settings. Select “Enable all macros” or digitally sign the macro.
| Feature | Built-in Find and Replace | VBA Macro |
|---|---|---|
| Automation | Manual per document | Batch across multiple documents |
| Wildcard support | Yes, via dialog | Yes, with MatchWildcards |
| Formatting replacement | Limited to font styles | Full control over font, paragraph, and other properties |
| Conditional logic | None | Can inspect each match before replacing |
| Reusability | Must reapply settings each time | Run the same macro anytime |
VBA gives you full control over find and replace operations that the built-in dialog cannot match. Start with simple replacements and add wildcards or formatting as you gain confidence. For advanced tasks, explore the Find object’s ClearFormatting and ClearAllFuzzyOptions methods to reset all search properties before setting new ones. This prevents leftover settings from interfering with your next search.