logo
search
Others

How to Highlight Words Longer Than 7 Characters with VBA Exceptions

Maira MehtabMaira Mehtab Sep 22, 2026 869 views

Question details

The user wants to use a VBA macro to find and highlight words with eight or more letters in a Word document, while applying a different highlight color to specific exception words.

Product
Word
Device & OS
not provided
Scenario
Automating document formatting to visually identify long words and mark specific exception words using different highlight colors.
Observed behavior
The macro highlights words correctly, but the user needs the proper syntax to format the exceptions list (like 'document' and 'graphics') and configure the macro's case sensitivity.
Before you start

Ensure you have the Developer tab enabled in your word processor to access the VBA editor, and always save a backup of your document before running new macros.

Solution 1Recommended

Format the Exceptions List with Pipe Characters

Properly formatting the exception string ensures the macro correctly identifies exact words that should receive an alternative highlight color.

To prevent partial word matches (for instance, matching 'graph' inside 'graphics'), the VBA macro uses pipe characters to separate the exception words.

1
Open the VBA Editor

Press ALT + F11 to open the VBA Editor and locate your highlighting macro.

2
Locate the Exceptions String

Find the variable in your code that defines the exception words.

3
Add Pipe Characters

Format your chosen exception words so they are separated and enclosed by pipe characters. For example, type `|document|graphics|`.

4
Run the Macro

Save your code and run the macro. Words longer than seven characters will be highlighted in the primary color, while 'document' and 'graphics' will receive the alternative color.

Exact Matching: Using pipe characters guarantees that the macro only applies the exception formatting to exact whole words.
Advanced Document Automation

Run Macros and Format Text Easily with WPS Writer

WPS Office provides advanced document formatting capabilities and supports running macros in its professional versions, allowing you to automate text highlighting seamlessly.

  1. 1. Open WPS Writer: Launch WPS Writer and open the document you wish to format.
  2. 2. Enable Developer Tools: Navigate to the Developer tab on the top ribbon menu.
  3. 3. Create a Macro: Click on 'Macros', type a name for your new macro, and click 'Create' to open the editor.
  4. 4. Paste and Run: Paste your VBA highlighting code into the editor, save it, and press Run to apply the colors.
Fully compatible with Microsoft Word .docx and .docm filesFamiliar Developer tab for macro and VBA managementLightweight and fast text processing for large documentsIntuitive interface for applying bulk formatting rules
microsoft office alternative - wps office

Frequently Asked Questions

Are the exception words in a Word VBA macro case-sensitive by default?

Yes, standard VBA string comparison is case-sensitive. 'Document' and 'document' are treated as two different words unless you modify the code to use functions like LCase() or add Option Compare Text.

How do I change the highlight color for the exception words?

In your VBA macro code, locate the HighlightColorIndex property applied to the exceptions list. You can change its value to your desired color, for example, from wdYellow to wdRed.

Why do I need to use pipe characters around the exception words?

Pipe characters (such as |document|) act as boundary markers. They prevent the macro from making partial matches, ensuring that only exact, whole words are treated as exceptions.

Can I add more than two words to the exceptions list?

Yes, you can add as many words as you need. Just ensure each word is enclosed by pipe characters in the string, for example: |document|graphics|example|formatting|.