logo
search
VBA & Macro Problems

How to Count Words by Font Color Using Word VBA

Elise WilliamsElise Williams Sep 27, 2026 870 views

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.

How to Count Words by Font Color Using Word VBA
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 you start

Before running any VBA macro, ensure that you have enabled macros in your Word Trust Center settings and saved a backup of your document.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor in Word.

2
Check RGB Syntax

Locate your color-formatting lines and ensure they use the correct RGB(R, G, B) syntax to define the font colors.

3
Fix Line Continuations

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.

4
Remove Redundant Procedures

Delete the unnecessary 'WordCountByColor' procedure if it duplicates your main code, and rename your main procedure (e.g., 'Demo') to a more descriptive name.

Correct VBA Syntax and Remove Blank Lines
Macro Security: If you are using Word 365, you may need to explicitly unblock the document or enable macros in the Trust Center for the code to execute successfully.
Advanced Word Processing Tools

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. 1. Download and Install: Download WPS Office for free from the official website and complete the quick installation process.
  2. 2. Open Your Document: Launch WPS Writer and open the Word document you want to analyze.
  3. 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. 4. Run Your Macro: Paste your corrected VBA code for counting font colors into a new module and click 'Run' to generate your report.
High compatibility with Microsoft Word documents (.doc, .docx) and VBA scripts.Built-in Developer tools for writing, debugging, and running macros smoothly.Lightweight design ensuring fast startup times and minimal system resource usage.Free version available with a rich set of professional word processing features.
microsoft office alternative - wps office

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.