How to Make Matching Text Bold and Red using Word VBA
Question details
The user needs to search for specific names from an array using a Word VBA macro and apply bold and red formatting to the matched text.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro to find multiple specific string matches from an array and highlighting them, but the current code attempts to format the Selection object and fails.
- Observed behavior
- Formatting the Selection object does not correctly apply the expected bold and red formatting to the text matches found via the array.
Ensure you have the Developer tab enabled to access the VBA editor. It is highly recommended to save a backup copy of your document before running bulk Find and Replace macros to prevent accidental loss of original formatting.
Apply Formatting via the Find Replacement Object
Instead of attempting to format the current Selection, apply the font formatting directly to the Find object's replacement properties and use the wdReplaceAll command.
When running a VBA loop that searches for items from an array, attempting to apply formatting directly to the 'Selection' often fails because the macro does not actively highlight each instance on the screen. The most efficient and reliable method is to configure the Find object to apply formatting during the replacement process.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor and locate the module containing your search array macro.
Before configuring the new search, ensure no residual parameters interfere by adding `Selection.Find.ClearFormatting` and `Selection.Find.Replacement.ClearFormatting` to your code.
Assign the desired text formatting directly to the replacement object by adding `Selection.Find.Replacement.Font.Bold = True` and `Selection.Find.Replacement.Font.Color = wdColorRed`.
Add `Selection.Find.Format = True` to instruct Word that the replacement operation should include formatting changes.
Trigger the bulk formatting by adding `.Execute Replace:=wdReplaceAll` inside your loop. Also, review your search array and remove duplicate names to prevent the macro from performing unnecessary and repeated processing.

Easily Run and Manage VBA Macros in WPS Office
WPS Office provides robust and professional support for VBA macros, allowing you to automate repetitive formatting tasks effortlessly with high compatibility for standard Word macro scripts.
- 1. Enable Developer Tools: Open WPS Writer and navigate to the 'Developer' tab on the top ribbon.
- 2. Open the VBA Editor: Click the 'Visual Basic' icon or press Alt + F11 to open the macro editor.
- 3. Run the Formatting Macro: Paste your optimized array search code, click 'Run' (or press F5), and instantly apply bold and red formatting to your matched text.

Frequently Asked Questions
Why does formatting the Selection fail when using an array in VBA?
When iterating through an array using a Find macro without actively selecting the found text on the screen, the Selection object remains in its original position. By applying formatting to the Replacement object instead, the macro correctly targets and formats the text as it performs the internal replace operation.
What is the correct VBA color code for red text?
In Word VBA, the standard built-in constant for red is `wdColorRed`. Alternatively, you can use exact RGB color values by assigning `RGB(255, 0, 0)` to the Font.Color property.
Do I need to clear the search formatting after the macro runs?
Yes. It is highly recommended to include `Selection.Find.ClearFormatting` and `Selection.Find.Replacement.ClearFormatting` at both the beginning and the end of your macro execution. This prevents these specific font and color settings from accidentally applying to subsequent manual searches in your document.




