How to Count Words by Font Color Using Word VBA
Question details
The user wants to create a Word VBA macro to count words based on their font color and append a formatted report at the end of the document.

- Product
- Microsoft Word
- Device & OS
- Windows and Mac
- Scenario
- Writing and executing a VBA macro to analyze document text and generate a color-coded word count report.
- Observed behavior
- The macro fails to run properly due to incorrect RGB formatting syntax, blank lines breaking continued VBA statements, or macro security settings blocking execution.
Before running any VBA macro, ensure that you have enabled macros in your Word Trust Center settings and saved a backup of your document.
Correct VBA Syntax and Remove Blank Lines
Fix the most common reasons the macro fails by updating RGB syntax and fixing line continuations.
When copying or editing VBA code, invisible formatting issues such as blank lines after line continuations can break the script. Additionally, using outdated or incorrect RGB output formats will prevent the color-counting logic from working properly.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in Word.
Locate your color-formatting lines and ensure they use the correct RGB(R, G, B) syntax to define the font colors.
Scan your code for the line continuation character (an underscore '_'). Delete any blank lines immediately following this character so the code is read as a single continuous statement.
Delete the unnecessary 'WordCountByColor' procedure if it duplicates your main code, and rename your main procedure (e.g., 'Demo') to a more descriptive name.

Output the Macro Results to the Document
Modify the macro to append the generated word count report directly to the end of the active document instead of displaying a message box.
Run Macros and Analyze Documents Seamlessly with WPS Office
WPS Office provides a powerful, built-in VBA environment that makes it easy to run macros like counting words by font color. It's a lightweight, fully compatible alternative to Microsoft Word.
- 1. Download and Install: Download WPS Office for free from the official website and complete the quick installation process.
- 2. Open Your Document: Launch WPS Writer and open the Word document you want to analyze.
- 3. Access Developer Tools: Navigate to the Developer tab on the top ribbon and click on 'Macros' or press Alt + F11 to open the VBA Editor.
- 4. Run Your Macro: Paste your corrected VBA code for counting font colors into a new module and click 'Run' to generate your report.

Frequently Asked Questions
Why do I get a syntax error when using line continuation in Word VBA?
If you place a blank line or a comment immediately after a line continuation character (an underscore '_'), VBA cannot parse the statement correctly. Ensure the next piece of code is directly on the following line without empty spaces in between.
How do I enable macros in Word 365 to run my script?
Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select 'Enable all macros' or 'Disable all macros with notification' to allow your VBA scripts to run. Remember to only enable macros from trusted sources.
Can I count words of a specific font color without using VBA?
While native Word features like Advanced Find can locate text by specific font formatting (including color), it cannot automatically generate a comprehensive count report for every color present. A VBA macro is required to automate and format this data into a report.




