colorindex as name

  • Thread starter Örjan Skoglösa
  • Start date
Ö

Örjan Skoglösa

When I run

Dim färg
färg = Selection.Shading.BackgroundPatternColorIndex
MsgBox färg

then I get the colorindex as a number.

Is it possible to return it as a constant name?

TIA
Örjan

(I´m on Word97)
 
J

Jonathan West

Örjan Skoglösa said:
When I run

Dim färg
färg = Selection.Shading.BackgroundPatternColorIndex
MsgBox färg

then I get the colorindex as a number.

Is it possible to return it as a constant name?

You create an array of names, and then use the number to look it up. There's
no automatic way.
 
Ö

Örjan Skoglösa

Thanks for your reply Jonathan.

I guess it is obvious, but sadly I do not understand completely.

I create an array with all the constants, e.g. by copying them from
the help, yes?

But how do I get the connection between numbers and names?

TIA
Örjan
 
J

Jonathan West

Put the constants into the array in the order of their value, like this

Dim vColors as variant
vColors = Array("wdAuto", "wdBlack", "wdBlue", "wdTurquise", _
"wdBrightGreen", "wdPink", "wdRed", "wdYellow", "wdWhite", _
"wdDarkBlue", "wdTeal", "wdGreen", "wdViolet", "wdDarkRed", _
"wdDarkYellow", "wdGray50", "wdGray25",)

Then you can return the color name like this

MsgBox vColors(Selection.Shading.BackgroundPatternColorIndex)
 
J

Jezebel

Add a function like this:

Public Function ColorIndexName(ColorIndex as WdColorIndex) as string

Select Case ColorIndex
Case wdAuto
ColorIndexName = "Auto"
Case wdBlack
ColorIndexName = "Black"
:
Case else
ColorIndex = "** Invalid ColorIndex"
end select

End Function

färg = ColorIndexName(Selection.Shading.BackgroundPatternColorIndex)

An array would also work in this case because the constants have sequential
values, but that's not the case with all sets of constants. Using Select
Case has the advantage that it doesn't matter what values the constants
actually represent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top