Consulting

Results 1 to 3 of 3

Thread: Remove specific highlight color in whole document

  1. #1

    Post Remove specific highlight color in whole document

    HI friend,

    I have the below code to undo/remove highlight, but this will remove at one location.

    can any one modify the code to removed specific highlighted color in entire document.

    Sub SearchAnyHighlight()

    Dim hiliRng As Range
    Set hiliRng = ActiveDocument.Content


    With hiliRng.Find
    .Highlight = True
    .Execute
    If hiliRng.Find.Found Then
    MsgBox "You can't close Active Document"


    Do While hiliRng.Find.Execute

    If hiliRng.HighlightColorIndex = wdBrightGreen Then
    hiliRng.HighlightColorIndex = wdNoHighlight
    End If
    Loop
    End If
    End With
    End Sub

  2. #2
    Documents are made up of various story ranges. If you want to process just the main document body then add
    hiliRng.Collapse 0
    after
    hiliRng.HighlightColorIndex = wdNoHighlight

    otherwise

    Sub SearchAnyHighlight()
    Dim hiliRng As Range
    Dim bFound As Boolean
    
        For Each hiliRng In ActiveDocument.StoryRanges
            With hiliRng.Find
                .Highlight = True
                .Execute
                If hiliRng.Find.Found Then
                    If Not bFound Then
                        MsgBox "You can't close Active Document"
                    End If
                    bFound = True
                End If
            End With
            Do While hiliRng.Find.Execute
                If hiliRng.HighlightColorIndex = wdBrightGreen Then
                    hiliRng.HighlightColorIndex = wdNoHighlight
                    hiliRng.Collapse 0
                End If
            Loop
            If hiliRng.StoryType <> wdMainTextStory Then
                While Not (hiliRng.NextStoryRange Is Nothing)
                    Set hiliRng = hiliRng.NextStoryRange
                    Do While hiliRng.Find.Execute
                        If hiliRng.HighlightColorIndex = wdBrightGreen Then
                            hiliRng.HighlightColorIndex = wdNoHighlight
                            hiliRng.Collapse 0
                        End If
                    Loop
                Wend
            End If
        Next hiliRng
        Set hiliRng = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Jul 2020
    Posts
    2
    Location

    Leaves first word highlighted

    Apologies for resurrecting a 6 year old thread, but it is directly relevant to using this code...

    This code works very well for removing a specific color of highlighting except for one catch - if the very first word of the document is highlighted in the specified color, that highlighting remains after the remainder of the highlights are removed. What should be adjusted to include the first word int he removals?
    Last edited by rekent; 08-01-2020 at 12:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •