logo
search
VBA & Macro Problems

How to Make Word VBA Treat Underscores as Part of a Word

WPS EditorWPS Editor Sep 30, 2026 869 views

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.

How to Make Word VBA Treat Underscores as Part of a Word
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 you start

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.

Solution 1Recommended

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.

1
Insert initial Find and Replace

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 (þ).

2
Run formatting code

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.

3
Restore underscores

At the end of your VBA macro, add a second Find and Replace command to change all placeholder characters (ß) back to underscores (_).

Use a Letter-Like Placeholder for Underscores
Choosing the Right Placeholder: Ensure that the placeholder character you select (e.g., ß or þ) is completely unique and does not naturally occur anywhere in your document text.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website to download and install the free version of WPS Office.
  2. 2. Open your macro-enabled document: Launch WPS Writer and open your existing .docm or .docx file directly without needing file conversion.
  3. 3. Run your macros: Enable macros in the security prompts and execute your VBA scripts within the familiar developer environment.
Free and lightweight word processing alternativeSeamless compatibility with Microsoft Word formats (.docx) and macro-enabled files (.docm)Familiar user interface for easy transitionRobust support for traditional VBA scripting and document automation
microsoft office alternative - wps office

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.