VBA: Change highlight color (RGB not wbColor) with keyboard shorcut (MS Word 2013)

10,417

I believe the answer to "Can we change the highlight color" is No. The HighlightColorIndex must refer to an "Applies to" list of constants. The set for the WdColorIndex constants (MSDN Word 2003) is …

wdAuto wdBlack wdBlue wdBrightGreen wdByAuthor wdDarkBlue wdDarkRed wdDarkYellow wdGray25 wdGray50 wdGreen wdNoHighlight wdPink wdRed wdTeal wdTurquoise wdViolet wdWhite wdYellow

This list is similar to an emuneration without the numbers, it is a set or collection that must be used with this particular limited property. There is no Highlight object that I have found referenced.

In the shading example given, the .shading "property" actually returns a shading object which can access the full RGB Color Model as your code above demonstrates and is referenced many places including https://msdn.microsoft.com/en-us/library/dd355244.aspx

The Office 2003 remarks for WdColorIndex explain the wdByAuthor entry in the list above.

"If the InsertedTextColor property is set to wdByAuthor, Microsoft Word automatically assigns a unique color to each of the first eight authors who revise a document."

Share:
10,417

Related videos on Youtube

MagTun
Author by

MagTun

Updated on September 18, 2022

Comments

  • MagTun
    MagTun over 1 year

    I have this module in VBA assigned to a keyboard shorcut to change the color of highlight:

    Sub RotateHighlightwbColor()
      Select Case Selection.Range.HighlightColorIndex
        Case wdYellow
            Selection.Range.HighlightColorIndex = wdGray25
        Case wdGray25
            Selection.Range.HighlightColorIndex = wdRed
        Case wdRed
            Selection.Range.HighlightColorIndex = wdPink 
        Case wdNoHighlight
            Selection.Range.HighlightColorIndex = wdYellow
        Case Else
            Selection.Range.HighlightColorIndex = wdNoHighlight
        End Select
    
    End Sub
    

    But instead of wbColor I want to use RGB color(more choices of color).

    I could find a way to do it but it uses Shading instead of Highlights.

    Sub RotateHighlightRGB()
    Select Case Selection.Font.Shading.BackgroundPatternColor
    
        Case RGB(255, 255, 255)
            Selection.Font.Shading.BackgroundPatternColor = RGB(1, 255, 1)
        Case RGB(1, 255, 1)
            Selection.Font.Shading.BackgroundPatternColor = RGB(0, 0, 0)
        Case RGB(0, 0, 0)
            Selection.Font.Shading.BackgroundPatternColor = RGB(255, 255, 255)
        Case Else
            Selection.Font.Shading.BackgroundPatternColor = RGB(255, 255, 255)
        End Select
    End Sub
    

    Is there a way to use RGB color with Selection.Range.HighlightColorIndex instead of using Shading?

    • Raystafarian
      Raystafarian about 9 years
      Don't use colorindex, just use color. Also see this
    • MagTun
      MagTun about 9 years
      I have tried Select Case Selection.Range.HighlightColor and then Selection.Range.HighlightColor = RGB(255, 255, 0) (for instance), but VBA gives me an error: Compile error method or data member not found. I have tried with Select Case Selection.Range.Interior.Color (as suggested on your link, but it seems it's only working with excel not with word)