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

1

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?

MagTun

Posted 2015-02-19T16:42:08.970

Reputation: 746

Don't use colorindex, just use color. Also see this

– Raystafarian – 2015-02-19T17:14:04.910

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) – MagTun – 2015-02-21T08:40:57.173

Answers

2

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."

Robert Hambly

Posted 2015-02-19T16:42:08.970

Reputation: 46