How to Highlight Word Text from an Excel List Using a VBA Macro
Question details
The user needs to create an Excel VBA macro that automates Word to find and highlight specific words listed in Excel columns (A through C), applying a unique color for each column.

- Product
- Microsoft Excel and Word
- Device & OS
- not provided
- Scenario
- Automating document formatting by searching a Word document for multiple specific terms stored in an Excel spreadsheet and applying highlight colors based on their column.
- Observed behavior
- The initial macro attempts fail with a 'Method or data member not found' compile error because the search logic is incorrect and HighlightColorIndex is improperly applied to wordDoc.Content.FormattedText.
Ensure you have your Excel list ready with the target words in columns A through C, and verify the exact file path of the Word document you intend to format.
Use a Duplicate Word Range and Apply HighlightColorIndex
The correct approach to highlighting words across applications using VBA is to declare a specific Word Range for the Find operation, apply the highlight color directly to matches, and collapse the range to continue.
When automating Word from Excel, especially using late binding, it is crucial to handle Word objects correctly. Applying formatting to the entire document content at once triggers compilation errors.
Instead, you must create a Range, perform the Find operation, apply the highlight color to that specific Range, and then collapse the Range to search for subsequent instances.
Open your Excel workbook containing the list of words. Press ALT + F11 to open the VBA Editor, click 'Insert' on the top menu, and select 'Module'.
Set up your macro using late binding to prevent version conflicts. Declare your application and document variables as Objects (e.g., Dim wordApp As Object, Dim wordDoc As Object) and initialize them using CreateObject("Word.Application").
Since late binding doesn't automatically recognize Word constants in Excel, manually define the numeric values for your colors at the top of your script: wdRed = 6, wdBrightGreen = 4, and wdBlue = 2.
Create a loop to read words from columns A:C. For each word, define a Word Range (e.g., Set rng = wordDoc.Content). Use a Do While rng.Find.Execute(word_from_excel) loop to find all instances.
Inside the Find loop, apply the color using rng.HighlightColorIndex = [YourColorConstant]. Then, immediately collapse the range to the end of the found word using rng.Collapse Direction:=0 (which equates to wdCollapseEnd) so the loop can find the next instance.

Try WPS Office for Seamless Spreadsheet and Document Management
If you are struggling with complex VBA macro setups and cross-application automation errors in Microsoft Office, consider switching to WPS Office. It provides a lightweight, highly compatible suite for all your document processing needs.
- 1. Download and Install: Visit the official WPS website, download the installer, and complete the lightweight installation process.
- 2. Open Your Excel and Word Files: Launch WPS Office and drag your existing .xlsx and .docx files into the workspace to continue your work without formatting loss.
- 3. Explore Advanced Features: Use WPS Spreadsheet's built-in advanced formatting and data processing tools to highlight data effortlessly without relying on complex cross-app macros.

Frequently Asked Questions
Why do I get a 'Method or data member not found' error in my Excel VBA macro?
This error occurs when you attempt to apply a property or method to an incompatible object. In this scenario, trying to set HighlightColorIndex on wordDoc.Content.FormattedText triggers the error because it must be applied to a valid Word.Range object.
What is late binding in Excel VBA?
Late binding is a coding technique where you declare external application objects (like Word) as generic Objects (e.g., Dim wordApp As Object) rather than referencing the specific Word Object Library. This prevents version-mismatch errors across different computers but requires you to manually define application-specific constants like wdRed.
How do I collapse a Word range in VBA after finding a word?
After your .Find.Execute method successfully locates a word and you apply the necessary formatting, use the Range.Collapse method with the direction wdCollapseEnd (or the numeric value 0 if using late binding). This moves the search starting point immediately past the found text, preventing infinite loops.




