Return current shading background color

P

Paul B

Hi,

I'm trying to Return the value of the currently selected
background shading color. This will be the color that was last
used for background shading. This will allow me to set the b/g
shading color on the fly, then use it persistently via code until
I manually change it.

Word knows that color because it is displayed on the Shading
icon, but the problem I'm running into is that VBA requires an
object to work from, and I've found no general object that allows
for b/g shading. So something like:

Dim shading1 As Shading
Dim returnValue As WdColor

returnValue = Shading1.BackgroundPatternColor

which comes courtesy of MSDN, faults out with a No Object error.

So, am I stuck with hard coding the shading color?

Thanks,
p.
 
G

George Lee

No, you’re not stuck with hard coding. Shading is part of another object (and
within a Range object), and it has to be assigned or Set first. A trip to the
Word help file (C:\Program Files\Microsoft Office\OFFICE11\1033\VBAWD10.CHM,
for example) shows there are 17 items that use the Shading object. Two of the
ways to do this is either access the property directly, or assign the object
first, then access the property.


Public Sub ShadingTest()
Dim returnValue As WdColor
returnValue = ActiveDocument.Range.Shading.BackgroundPatternColor
ActiveDocument.Range.Shading.BackgroundPatternColor = wdColorBlue

Dim Shading1 As Shading
Set Shading1 = ActiveDocument.Range.Shading
returnValue = Shading1.BackgroundPatternColor
Shading1.BackgroundPatternColor = wdColorLime
End Sub
 
P

Paul B

Thanks, George. Now I see all those objects upstream of Shading.

When I run your code, I'm not picking up the current Shading
color. I think the line

returnValue = ActiveDocument.Range.Shading.BackgroundPatternColor

picks up the shading of the entire doc, which happens to be none.
The last line hard codes the shading color, irrespective of
returnValue, thereby covering the shortcoming. So I'm back where
I was.

The highlight object is a member of the options object, so that

currentvalue = Options.DefaultHighlightColorIndex
Selection.Range.HighlightColorIndex = currentvalue

does what I'm trying to do with Shading (which has a far superior
color selection than Highlighting). What I think I'm lacking is a
general object, such as Options or Application, which would read
the Shading color as set in the UI.

Am I missing something? Very possible.
p.
 
G

George Lee

The code sample wasn’t meant to be complete, just how to get and set values.
Your program logic will have to calculate which values to use.

I may be missing what you’re trying to do. The code shows that you can get
and set the color of your object, whichever one that is (such as table, row,
cell, or the document as a whole). You can get the currently set highlight
color. Not all objects have that global setting, other than the default Word
sets for it (for example, the document’s background color is nothing), unless
you explicitly change it. If you want every fifth table row to be red, or if
a cell value that is greater than 0 (for example) to be lime green, you’ll
have to set a variable yourself and use program logic to add the color.

Does that help?
 
T

Tony Jollans

I don't know any easy way to do this but it might be possible to do
something with the shading control. Unfortunately, split buttons are not
easy to work with but perhaps you could examine the tooltip text. It would
still take quite a bit of coding, something alomng these lines:

TTText = CommandBars.FindControl(ID:=2947).TooltipText
TTColour = Mid(TTText, InStr(TTText, "(") + 1)
TTColour = Left(TTColour, Len(TTColour) - 1)
If Left(TTColour, 3) = "RGB" Then
TTRGB = Split(Mid(TTColour, 5, Len(TTColour) - 5), ",")
Selection.Shading.BackgroundPatternColor = _
RGB(TTRGB(0), TTRGB(1), TTRGB(2))
Else
Select Case TTColour
Case "Red": Selection.Shading.BackgroundPatternColor = wdColorRed
Case "Yellow": Selection.Shading.BackgroundPatternColor =
wdColorYellow
Case "Gray-20%": Selection.Shading.BackgroundPatternColor =
wdColorGray20
' etc.
' etc.
End Select
End If
 
P

Paul B

Wow, that is an insightful piece of coding. It works perfectly
for RGB, but I think a more concise way for named colors would be
something like:

Else
ttcolour = "WdColor" + ttcolour
Selection.Shading.BackgroundPatternColor = ttcolour
End If

The problem I'm having is that the concatenated variable is still
a string, encased with quotes, so Word chokes on it. Somehow the
string has to be converted to some other kind of variable.

Does that sound right?

Thanks for some great input.
p.
 
P

Paul B

Hi George,

The problem is that the object whose color I want to pick up is
the application's Shading Tool, not any object in the document.
This is where Word's vba, on its face, falls short. But see Tony
Jollan's post right after yours for a way into the fortress.

Thanks,
p.
 
T

Tony Jollans

Yes, you're right. The Word constants are needed at compile time, but you
don't have them till run time. There are two ways round this that I believe
should work, neither of them easy.

1. Dynamically create a VBA function when you have the string. This is
possible in your own environment if you have "Trust Access to VBA Project"
set, but not really viable to roll out to other users.

2. Use the Typelib Information Typelib library to look up the Word
constants. This would be the easiest if you have the library (distributed
with VB6 I think, although it may be available somewhere for download, I'm
not sure). Again, I suppose, this isn't easy to roll out to other users who
may not have the library.

Both methods will need special coding for things like "Gray-25%", which
don't translate directly.

All in all, hard coding the sixty or so literals is probably best, but if
you want to have a bash at the other methods, come back.
 
P

Paul B

Ok, thanks for that insight. I thought there might be a way to
convert a string to another type of variable, one without quote
marks, but it seems not.

Probably I would only need about a dozen named colors to work
with (the reason I like the Shading function is that it offers
colors much less saturated than Word's horrible Highlight colors,
and a handful or so of them would be very sufficient), so
hardcoding a practical palette offering wouldn't be too
difficult. And I could put in a catch-all for any named colors
not listed.

Thanks for your help; I think I'm well on my way.
Be well,
p.
 

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