VBA & Macro Problems

Replace Red Underlined Text with Blank Lines in Word VBA

Amos GikundaAmos Gikunda Nov 27, 2026 869 views

Question details

Replace red, underlined text in a Word document with underscore blanks of a similar or greater length.

Replace Red Underlined Text with Blank Lines in Word VBA
Product
Microsoft Word VBA
Device & OS
not provided
Scenario
A worksheet author wants to turn formatted phrases into fill-in-the-blank spaces for students.
Observed behavior
Each matching phrase should become an unformatted underscore line, with optional extra characters to make the blank wider.
Before you start

Open the document in a desktop version of Microsoft Word that supports VBA, and check that the target text uses red font and single underlining. Run the macro on a copy first, and enable macros only if you trust the document and code.

Solution 1Recommended

Run a Word VBA macro to replace matching text

Use this method to replace red, single-underlined text with underscores and add extra character widths.

The macro searches for explicitly red text with single underlining. It skips any match containing a paragraph or table-cell marker so that it does not remove document structure.

The underscore count is based on character count, not the displayed width of the original phrase. Formatting boundaries can also cause a phrase to be treated as separate matches.

1
Open the VBA editor

In Word, open the document copy and press Alt+F11 to open the Visual Basic Editor. Select Insert > Module.

2
Insert the macro

Paste this code into the module: Sub ReplaceRedUnderlinedText() Dim r As Range Dim blankLength As Long Set r = ActiveDocument.Content With r.Find .ClearFormatting .Text = "" .Forward = True .Wrap = wdFindStop .Format = True .MatchWildcards = False .Font.Color = wdColorRed .Font.Underline = wdUnderlineSingle End With Do While r.Find.Execute If InStr(r.Text, vbCr) = 0 And InStr(r.Text, Chr(7)) = 0 Then blankLength = Len(r.Text) + 2 r.Text = String(blankLength, "_") r.Font.Color = wdColorAutomatic r.Font.Underline = wdUnderlineNone End If r.Collapse Direction:=wdCollapseEnd Loop End Sub

3
Run and review

Return to Word, press Alt+F8, select ReplaceRedUnderlinedText, and click Run. Review the blanks and any matching text that was skipped.

4
Adjust the blank length

In the VBA editor, change the 2 in `blankLength = Len(r.Text) + 2` to 0 for the same character count, or to 3 or more for wider blanks. Run the revised macro on a fresh copy.

Run a Word VBA macro to replace matching text
Keep the macro: To retain the VBA code, use File > Save As and choose Word Macro-Enabled Document (*.docm).
Free Microsoft Office alternative

Edit worksheet documents with WPS Writer

WPS Office is a free, lightweight, easy-to-use Office alternative with a familiar interface. Open compatible Word documents in WPS Writer to continue editing worksheets; it does not fix Microsoft Word VBA issues.

Edit and format fill-in-the-blank worksheets in WPS WriterCompatible with Microsoft Word DOC and DOCX formatsFamiliar interface for a smoother move from Microsoft OfficeFree and lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

How can I make the underscore blanks wider?

In the macro, increase the number in `Len(r.Text) + 2` to 3 or more. Each additional number adds one underscore character.

Why did the macro miss some underlined text?

The Find settings match explicitly red font with single underlining. Check the text's font color and underline style in Word's Home tab; the macro also skips matches containing paragraph or table-cell markers.

Will an equal number of underscores be the same visual width as the phrase?

Not necessarily. The macro uses the number of characters, but letters and underscores can have different displayed widths depending on the font.

How do I save the document with the macro?

In Word, select File > Save As and choose Word Macro-Enabled Document (*.docm). A DOCX file cannot retain VBA macros.