How to Make Word VBA Treat Underscores as Part of a Word
Question details
The user needs to format specific text strings containing underscores (like s_Person), but Word's VBA Words collection splits the text at the underscore.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Running a VBA formatting macro on targeted document text that includes underscores.
- Observed behavior
- Word treats the underscore as punctuation and returns it as a separate item in the Words collection, making targeted string formatting difficult.
Before running a document-wide Find and Replace script, isolate a small paragraph of text containing underscores to test your macro. This ensures the placeholder character you have chosen does not conflict with existing document content.
Use a Letter-Like Placeholder for Underscores
Temporarily replace all underscores with a unique alphabetical character before executing the main macro so Word reads the string as a single word.
The Words collection in Microsoft Word inherently treats all punctuation, including underscores, as word boundaries. By substituting the underscore with a rare alphabetical character, the Words collection will process the entire string as one continuous word.
At the beginning of your VBA macro, add a Find and Replace command to change all underscores (_) to a unique letter-like placeholder, such as a sharp s (ß) or thorn (þ).
Insert your intended text formatting code immediately after the replacement step. The macro will now successfully target the entire text string without breaking at the placeholder.
At the end of your VBA macro, add a second Find and Replace command to change all placeholder characters (ß) back to underscores (_).

Declare Variables Explicitly as Strings
Ensure variables and arguments are correctly defined as Strings to prevent errors when passing text to procedures.
Try WPS Office for Your Document Automation Needs
If you frequently encounter limitations with Microsoft Word's strict object collections and word boundaries, consider trying WPS Office. It provides a lightweight, highly compatible alternative with excellent support for VBA macros and complex document formatting.
- 1. Download WPS Office: Visit the official WPS website to download and install the free version of WPS Office.
- 2. Open your macro-enabled document: Launch WPS Writer and open your existing .docm or .docx file directly without needing file conversion.
- 3. Run your macros: Enable macros in the security prompts and execute your VBA scripts within the familiar developer environment.

Frequently Asked Questions
Why does Word VBA split words at the underscore?
The Microsoft Word VBA Words collection is programmed to treat the underscore character as punctuation rather than part of an alphabetical string. Therefore, it acts as a word boundary, causing text like 's_Person' to be read as three distinct items: 's', '_', and 'Person'.
Can I change Word's word boundary settings?
No, unlike some advanced text editors such as Notepad++, Microsoft Word does not offer native configuration options to redefine what constitutes a word boundary or punctuation within its Words collection.
What is the best placeholder to use in a VBA Find and Replace?
It is highly recommended to use unique, letter-like characters such as a sharp s (ß) or a thorn (þ). Because these are categorized as alphabetical letters, the Words collection groups them with surrounding text, and they rarely appear in standard English documents.




