Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-01-2020, 07:19 AM
rekent rekent is offline Remove highlight with specific color Mac OS X Remove highlight with specific color Office for Mac 2011
Novice
Remove highlight with specific color
 
Join Date: May 2014
Posts: 5
rekent is on a distinguished road
Default Remove highlight with specific color

I am using the below code to remove turquoise highlight from my document and it works well with one exception - it will not remove highlighting from the very first word of the document if that word is highlighted. Can anyone point me to the correction that I should make to fix that bug? Credit to forum member gmayor for this code found over on VBA Express.



Code:
Sub ClearHighlight()
  Dim hiliRng As Range

    For Each hiliRng In ActiveDocument.StoryRanges
        With hiliRng.Find
            .Highlight = True
            .Execute
        End With

        Do While hiliRng.Find.Execute
            If hiliRng.HighlightColorIndex = wdTurquoise 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 = wdTurquoise Then
                        hiliRng.HighlightColorIndex = wdNoHighlight
                        hiliRng.Collapse 0
                    End If
                Loop
            Wend
        End If
    
    Next hiliRng
    Set hiliRng = Nothing

End Sub
Reply With Quote
  #2  
Old 08-01-2020, 09:51 AM
gmaxey gmaxey is offline Remove highlight with specific color Windows 10 Remove highlight with specific color Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,079
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

It isn't a bug. You are explicitly ignoring the first instance found.


Code:
Sub ClearSpecificHighlight()
Dim oRngStory As Range
  For Each oRngStory In ActiveDocument.StoryRanges
    Do
      With oRngStory.Find
        .Highlight = True
        Do While .Execute
          If oRngStory.HighlightColorIndex = wdTurquoise Then
             oRngStory.HighlightColorIndex = wdNoHighlight
             oRngStory.Collapse 0
          End If
        Loop
      End With
      'Get next linked story (if any)
      Set oRngStory = oRngStory.NextStoryRange
    Loop Until oRngStory Is Nothing
  Next
lbl_exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 08-01-2020, 10:59 AM
rekent rekent is offline Remove highlight with specific color Windows 7 64bit Remove highlight with specific color Office 2010
Novice
Remove highlight with specific color
 
Join Date: May 2014
Posts: 5
rekent is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
It isn't a bug. You are explicitly ignoring the first instance found.
I should have been more clear that I meant bug for my particular use case, not code bug. I wasn't familiar enough with the way that stories are handled to see the issue myself. However, your solution solved my problem, thank you!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove highlight with specific color Find/remove underline with specific color eduzs Word VBA 9 07-27-2020 07:43 AM
VBA to highlight specific values in the spreadsheet daiwuliz Excel 6 05-23-2018 10:30 AM
Remove highlight with specific color Remove white text background (keeping the font color and page color intact cc3125 Word 1 10-26-2015 06:44 PM
Change highlight color maceslin Word 3 04-30-2013 04:43 AM
Remove highlight with specific color change One highlight Color to Another Redbarn Word VBA 1 03-16-2013 02:03 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 08:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft