How to Use Word VBA to Replace Whole Words or Exact Sequences
Question details
The user needs a Word VBA macro that replaces complete words or specific exact number sequences without altering longer words that contain the search text as a partial match.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Executing a VBA find-and-replace macro to accurately replace specific whole words (like "for") while ignoring instances where the text is embedded within larger words (like "form").
- Observed behavior
- Standard VBA find-and-replace partially matches text inside longer strings, and custom punctuation like brackets can interfere with standard word-boundary detection.
Ensure you have the Developer tab enabled to access the VBA editor and strongly consider backing up your document before running bulk replacement macros.
Use Wildcard Word Boundaries in VBA
Implement wildcard word boundary markers in your VBA code to force the macro to recognize and replace only exact whole words.
By default, standard search text strings match any occurrence of the text, even if it is part of another word. To prevent this, you can wrap your target search text in wildcard word boundaries (< and >).
Note that punctuation marks, such as brackets around values like [AAA_0001], are generally not recognized as standard word boundaries and may require specific character escaping in a wildcard search.
Press Alt + F11 on your keyboard to launch the Microsoft Visual Basic for Applications editor in Word.
In the Project Explorer panel, double-click the Module containing your specific find-and-replace macro script.
Modify the search text parameter in your code. Change your standard assignment from '.Text = rFindText.Text' to '.Text = "<" & rFindText.Text & ">"'.
Ensure that your Find object has the MatchWildcards property enabled by adding '.MatchWildcards = True' in your With selection.
If you are targeting bracketed texts or alphanumeric codes containing underscores, test these edge cases separately, as you may need to add backslashes to escape the brackets (e.g., '\[AAA_0001\]').

Replace Whole Words Safely with WPS Writer
While VBA provides automated replacement tools, you can easily perform exact whole-word and wildcard replacements directly through the intuitive WPS Writer interface, completely avoiding the need for manual coding.
- 1. Open Find and Replace: Launch WPS Writer and press Ctrl + H to bring up the Find and Replace dialog.
- 2. Expand Search Options: Click the 'Options' or 'More' button to reveal the advanced search criteria.
- 3. Select Whole Words: Check the box for 'Find whole words only' or 'Use wildcards' depending on whether you are replacing standard words or bracketed sequences.
- 4. Execute the Replacement: Enter your target text and click 'Replace All' to safely apply the exact matches across your document.

Frequently Asked Questions
Why does my VBA macro replace partial text inside longer words?
By default, a VBA text search looks for the sequence of characters regardless of their position. If you search for 'for', it will naturally match the first three letters of 'form'. You must explicitly define word boundaries or set '.MatchWholeWord = True' to prevent this.
What do the less-than and greater-than signs (< >) do in Word VBA?
When '.MatchWildcards = True' is active, the '<' sign indicates the beginning of a word, and the '>' sign indicates the end of a word. Wrapping your search text in these characters ensures the macro only matches standalone words.
How do I find text containing brackets like [123] using wildcards?
Brackets are reserved characters in wildcard searches used to define ranges (like [a-z]). To search for literal brackets in the document, you must place a backslash before them in your search string (e.g., '\[123\]').
Should I use .MatchWholeWord instead of wildcards?
If you are strictly replacing plain alphanumeric words without special punctuation, setting '.MatchWholeWord = True' in your VBA Find parameter is a simpler approach. However, if your sequence contains underscores or hyphens, wildcards offer much stricter control over the boundaries.




