VBA & Macro Problems

How to Make Matching Text Bold and Red using Word VBA

Muhammad TalhaMuhammad Talha Oct 26, 2026 869 views

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.

How to Make Matching Text Bold and Red using Word VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor and locate the module containing your search array macro.

2
Clear Previous Formatting

Before configuring the new search, ensure no residual parameters interfere by adding `Selection.Find.ClearFormatting` and `Selection.Find.Replacement.ClearFormatting` to your code.

3
Set Replacement Font Properties

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`.

4
Enable Format Application

Add `Selection.Find.Format = True` to instruct Word that the replacement operation should include formatting changes.

5
Execute the Replace Command

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.

Apply Formatting via the Find Replacement Object
Optimization Tip: Removing duplicate names from your array is crucial. Not only does it make the code cleaner, but it significantly speeds up macro execution times in large documents.
Advanced Document Processing with WPS Writer

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. 1. Enable Developer Tools: Open WPS Writer and navigate to the 'Developer' tab on the top ribbon.
  2. 2. Open the VBA Editor: Click the 'Visual Basic' icon or press Alt + F11 to open the macro editor.
  3. 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.
Fully compatible with Microsoft Word .doc, .docx, and .docm formatsSeamless execution of VBA macros for batch text replacement and formattingLightweight, fast, and features a familiar user interfaceAn excellent cost-effective solution for advanced document management
microsoft office alternative - wps office

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.