Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2023, 06:09 AM
syl3786 syl3786 is offline VBA Code to remove specific highlight Windows 10 VBA Code to remove specific highlight Office 2019
Advanced Beginner
VBA Code to remove specific highlight
 
Join Date: Jan 2023
Posts: 68
syl3786 is on a distinguished road
Default VBA Code to remove specific highlight

Hi Community,



This is a macro for removing specific highlight in Word Documents. When you place the cursor at the highlighted text (e.g., it highlighted wdYellow), then ran the macro, it can remove all wdYellow highlights in that word document.

However, I found a bug in the "Removeonehighlight" macro. When there are words highlighted with two colors and connected each other like 123456, the macro cannot remove the highlight color, neither red or yellow one. (see the attached word file)

The code are as follows:

Code:
Sub Removeonehighlight()

    Dim HLColor As WdColorIndex
    Dim rg As Range

    HLColor = Selection.Range.HighlightColorIndex
    If HLColor <> wdNoHighlight Then
    
        Set rg = ActiveDocument.Range
        
        With rg.Find
            .Highlight = HLColor
            .Wrap = wdFindStop
            
            While .Execute
                If rg.HighlightColorIndex = HLColor Then
                    rg.HighlightColorIndex = wdNoHighlight
                End If
                rg.Collapse wdCollapseEnd
            Wend
            
        End With
        
    Else

        MsgBox "Please place you cursor on the highlight color you want to remove."
    End If
        
End Sub
And I tried many other macros for removing specific highlight colours but in vain. All of them encounter the same issue. Does any experts know how to edit the above codes so that I can use it to remove highlight colors, even though a text was highlighted in two colours?

Your help would be greatly appreciated!
Attached Files
File Type: docx test.docx (12.1 KB, 3 views)
Reply With Quote
  #2  
Old 03-27-2023, 06:55 AM
gmaxey gmaxey is offline VBA Code to remove specific highlight Windows 10 VBA Code to remove specific highlight Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Highlight = True
    While .Execute
      Select Case oRng.HighlightColorIndex
        Case Is = 9999999
          oRng.HighlightColorIndex = wdAuto
      End Select
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 03-27-2023, 08:21 AM
syl3786 syl3786 is offline VBA Code to remove specific highlight Windows 10 VBA Code to remove specific highlight Office 2019
Advanced Beginner
VBA Code to remove specific highlight
 
Join Date: Jan 2023
Posts: 68
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Highlight = True
    While .Execute
      Select Case oRng.HighlightColorIndex
        Case Is = 9999999
          oRng.HighlightColorIndex = wdAuto
      End Select
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
Thank you gmaxey! It is very useful. MayI ask if this macro can just remove the one of the selected highlight colours but not remove all of them? Now it just remove texts with two highlighted colors combined and remove those combination.
Reply With Quote
  #4  
Old 03-27-2023, 08:57 AM
gmaxey gmaxey is offline VBA Code to remove specific highlight Windows 10 VBA Code to remove specific highlight Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

First, while a common mistake, I am not Gmayor.


If you want to get rid of red highlighting in a mix of red and some other color:

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
Dim oChr As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Highlight = True
    While .Execute
      Select Case oRng.HighlightColorIndex
        Case Is = 9999999
          For Each oChr In oRng.Characters
            If oChr.HighlightColorIndex = wdRed Then
              oChr.HighlightColorIndex = wdAuto
            End If
          Next
        Case Is = wdRed
          oRng.HighlightColorIndex = wdAuto
      End Select
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
or like this:


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
Dim oChr As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "*"
    .MatchWildcards = True
    .Highlight = True
    While .Execute
      Select Case oRng.HighlightColorIndex
        Case Is = wdRed
          oRng.HighlightColorIndex = wdAuto
      End Select
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 03-27-2023, 05:01 PM
syl3786 syl3786 is offline VBA Code to remove specific highlight Windows 10 VBA Code to remove specific highlight Office 2019
Advanced Beginner
VBA Code to remove specific highlight
 
Join Date: Jan 2023
Posts: 68
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
First, while a common mistake, I am not Gmayor.


If you want to get rid of red highlighting in a mix of red and some other color:

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
Dim oChr As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Highlight = True
    While .Execute
      Select Case oRng.HighlightColorIndex
        Case Is = 9999999
          For Each oChr In oRng.Characters
            If oChr.HighlightColorIndex = wdRed Then
              oChr.HighlightColorIndex = wdAuto
            End If
          Next
        Case Is = wdRed
          oRng.HighlightColorIndex = wdAuto
      End Select
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
or like this:


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
Dim oChr As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "*"
    .MatchWildcards = True
    .Highlight = True
    While .Execute
      Select Case oRng.HighlightColorIndex
        Case Is = wdRed
          oRng.HighlightColorIndex = wdAuto
      End Select
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
I deeply apologize for called the wrong name. I made a mistake. Thank you so much for your help! They works well!
Reply With Quote
Reply

Tags
highlight, macro, word vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Code to remove specific highlight VBA Highlight numbers only after or before specific words help Shelley Lou Word VBA 12 08-09-2022 03:02 AM
VBA Code to remove specific highlight Remove highlight with specific color rekent Word VBA 2 08-01-2020 10:59 AM
How to highlight lines containing specific words SixStringSW Word VBA 4 06-03-2018 03:57 PM
VBA to highlight specific values in the spreadsheet daiwuliz Excel 6 05-23-2018 10:30 AM
Highlight the current cell without using code parshla Excel 0 09-08-2016 08:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:40 AM.


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