Use a Word VBA UserForm to Highlight Text by Style
Question details
Use a Word VBA UserForm selection to apply or remove highlighting on text with a named style.
- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A user selects a highlight color in a UserForm and wants to format every matching occurrence of a specific style.
- Observed behavior
- Text matching the named style is highlighted or has its highlighting removed while its content remains unchanged.
Open the document in Microsoft Word and confirm the exact style name and the names of your UserForm controls. Save the VBA project in a macro-enabled document or template and enable macros only if you trust the file.
Replace all matching style formatting using the selected highlight color
Use this approach to apply or remove highlighting across the main document while preserving the matched text.
The procedure searches the main document story from its beginning. It clears existing Find and replacement formatting, finds the named style, and uses ^& as the replacement text to retain each match.
Open the VBA editor with Alt+F11, choose Insert > Module, and add this procedure: Public Sub HighlightStyle(ByVal styleName As String, ByVal colorIndex As WdColorIndex) Dim doc As Document Dim searchRange As Range Dim previousColor As WdColorIndex Set doc = ActiveDocument Set searchRange = doc.Content previousColor = Options.DefaultHighlightColorIndex On Error GoTo CleanUp If colorIndex <> wdNoHighlight Then Options.DefaultHighlightColorIndex = colorIndex End If With searchRange.Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "^&" .Style = doc.Styles(styleName) .Replacement.Highlight = (colorIndex <> wdNoHighlight) .Forward = True .Wrap = wdFindStop .Format = True .MatchWildcards = False .Execute Replace:=wdReplaceAll End With CleanUp: Options.DefaultHighlightColorIndex = previousColor If Err.Number <> 0 Then MsgBox "Highlighting failed: " & Err.Description, vbExclamation End Sub
In the VBA editor, open your UserForm and double-click its Apply button to create a Click event. For a ComboBox named cmbColor and a button named cmdApply, use: Private Sub cmdApply_Click() Dim chosenColor As WdColorIndex Select Case Me.cmbColor.Value Case "Yellow": chosenColor = wdYellow Case "Green": chosenColor = wdBrightGreen Case "None": chosenColor = wdNoHighlight Case Else MsgBox "Select Yellow, Green, or None." Exit Sub End Select HighlightStyle "Emphasis", chosenColor End Sub
Replace Emphasis with the exact name of the style in your document, and change cmbColor and cmdApply to your actual control names. Add Yellow, Green, and None to the ComboBox choices.
Open the UserForm, choose a color, and click Apply. Check styled text near the beginning and end of the document; choose None and click Apply to remove highlighting from matches.
Edit Word documents with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents, making it easy to move everyday document work between suites. It does not fix a Microsoft Word VBA UserForm issue; macro support can vary by platform and version.

Frequently Asked Questions
Why does the replacement use ^&?
In Word Find and Replace, ^& represents the text that was found. Setting .Replacement.Text to "^&" preserves that text while changing its highlighting.
How can the UserForm remove highlighting instead?
Pass wdNoHighlight to HighlightStyle. The procedure sets .Replacement.Highlight to False, removing highlighting from text that matches the named style.
Why are headers and footers not highlighted?
The example searches ActiveDocument.Content, which covers the main document story. To include headers, footers, or other stories, run an equivalent search on their respective story ranges.
What should I check if the macro finds no matches?
Check that the style name passed to HighlightStyle exactly matches a style in the document, and verify that the text actually uses that style. Also confirm that .Format is True and both Find and replacement formatting are cleared before the style is set.




