2

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?

2
  • Don't use colorindex, just use color. Also see this Feb 19, 2015 at 17:14
  • 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
    Feb 21, 2015 at 8:40

2 Answers 2

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

0

Yes, there is. See the interior.color property.

for example:

    WB.Worksheets(1).Rows(1).Interior.Color = RGB(255, 0, 255)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .