Reactor4
Reactor4

Reputation: 111

How to highlight selected text if not repeated?

I am using a VBA script (thanks to @MacroPod) to find the duplicates of a selected text in a document, and make them bold and red.

I want to update this script to see if certain sources referenced in text are not cited at the end. Is there any way to change the format of the selected text (such as making it pink) if that word is not repeated?

Sub MakeBold()
Application.ScreenUpdating = False
With ActiveDocument.Range
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Font.Bold = True
        .Replacement.Font.ColorIndex = wdRed
        .Text = Selection.Text
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Execute Replace:=wdReplaceAll
    End With
End With
Application.ScreenUpdating = True
End Sub

Upvotes: 0

Views: 49

Answers (1)

macropod
macropod

Reputation: 13490

If you used Word's referencing tools, that wouldn't be an issue. That said, try:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Text = Selection.Text
    .Forward = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found
    i = i + 1
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
  With .Find
    .Replacement.Text = "^&"
    .Replacement.ClearFormatting
    .Replacement.Font.Bold = True
    If i = 1 Then
      .Replacement.Font.ColorIndex = wdPink
    Else
      .Replacement.Font.ColorIndex = wdRed
    End If
    .Wrap = wdFindContinue
    .Format = True
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub

Upvotes: 1

Related Questions