Home > Software design >  How to find all instances of highlighted text in the active document and removes the highlight forma
How to find all instances of highlighted text in the active document and removes the highlight forma

Time:10-18

How do you find all instances of the highlighted text in the active document and remove the highlight formatting with mark-up?

I found a macro in https://learn.microsoft.com/en-us/office/vba/api/word.find.highlight. But I want it to remove the highlight formatting with mark-up. I tried to add " ActiveDocument.TrackRevisions = True" to turn on the Track Changes but in vain.

Sub A()
    Dim rngTemp As Range 
 
    Set rngTemp = ActiveDocument.Range(Start:=0, End:=0) 
    With rngTemp.Find 
     .ClearFormatting 
     .Highlight = True 
     With .Replacement 
     .ClearFormatting 
     .Highlight = False 
     End With 
     .Execute Replace:=wdReplaceAll, Forward:=True, FindText:="", _ 
     ReplaceWith:="", Format:=True 
    
    End With

End Sub 

Then I tried to record a Macro and edited it as follows:

Sub Macro1()

    Selection.Find.ClearFormatting
    Selection.Find.Highlight = True
    With Selection.Find
        .Text = ""
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Options.DefaultHighlightColorIndex = wdNoHighlight
    Selection.Range.HighlightColorIndex = wdNoHighlight
    Selection.Find.Execute
        
End Sub

The second one can only change highlighted text to no highlight with mark-up one by one. It is not convenient since I have at least 200 highlighted texts to decide whether they should be corrected in a document. How can I edit it to automatically select all highlighted text and then remove their highlights with mark-ups?

CodePudding user response:

Sub FindRemoveHighlighting()
    Dim findRange As Range: Set findRange = ActiveDocument.Content
    ActiveDocument.TrackRevisions = True
    With findRange
        With .Find
            .Highlight = True
            .Text = ""
            .Format = True
        End With
        Do While .Find.Execute() = True
            .HighlightColorIndex = wdNoHighlight
            .Collapse wdCollapseEnd
        Loop
    End With
End Sub
  • Related