user2576682
user2576682

Reputation: 123

How to change the background color of specific words?

I am trying to change the background color of specific words.

I have SQL code and I am trying to highlight all fields from the query in bright green. My plan is to enter field names and have those terms highlighted in bright green.

If I take the If statement out, the first line will be highlighted in bright green.
I have over 3200 words to go through.

Dim oSentence As Variant
Dim strSentence As String

strSentence = "Package_Policy_TA"

For Each oSentence In ActiveDocument.Sentences
    Debug.Print oSentence

    If oSentence = strSentence Then

         Stop
         oSentence.Font.Shading.BackgroundPatternColorIndex = wdBrightGreen

     End If

Next oSentence

Upvotes: 0

Views: 581

Answers (1)

macropod
macropod

Reputation: 13490

For example:

Sub HiliteWords()
Application.ScreenUpdating = False
Dim ArrFnd As Variant, i As Long
'Array of Find expressions
ArrFnd = Array("policy_Id", "policy_payer_option", "rpt_coverage", "coverage_id")
With ActiveDocument
  For i = 0 To UBound(ArrFnd)
    With .Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = False
        .Forward = True
        .Wrap = wdFindStop
        .MatchCase = True
        .MatchWholeWord = True
        .Text = ArrFnd(i)
        .Replacement.Text = ""
      End With
      Do While .Find.Execute
        .Font.Shading.BackgroundPatternColorIndex = wdBrightGreen
        .Collapse wdCollapseEnd
      Loop
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub

Upvotes: 1

Related Questions