Conditional Cell Color

W

WillW

I saw some other questions regarding conditional formating in Word tables, so
I don't hold out much hope for this. But it seems simple enough that there
might be some relatively easy way to do it.

Here's the situation: In a cell in the top row of a table in my Word
template, only 3 values are possible to select using a control: Green,
Yellow, Red. Can I make Word change the cell fill color based on the option
selected?

I tried using an embedded Excel table, but it just doesn't seem to be the
right solution in this case, and my manager doesn't want to use Excel for the
whole document as I suggested.

Thanks
 
G

Graham Mayor

If you use legacy form fields in your document, as suggested by Doug in
response to your other query, and one of those fields is a dropdown fiild
containing the three values, then you can certainly format a cell of the
table based on the selected content of that dropdown field by running a
macro on exit from the field. The following, for example, will colour the
first cell of the table .Cell(1, 1) according to the result of the field

Sub ColorCellA1()
Dim sColor As String
Dim bProtected As Boolean
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
bProtected = True
.Unprotect Password:=""
End If
With .Tables(1).Cell(1, 1).Shading
Select Case LCase(ActiveDocument.FormFields("Dropdown1").Result)
Case Is = "red"
.BackgroundPatternColor = wdColorRed
Case Is = "yellow"
.BackgroundPatternColor = wdColorYellow
Case Is = "green"
.BackgroundPatternColor = wdColorGreen
End Select
End With
If bProtected = True Then
.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End With
End Sub

You may find the examples at http://www.gmayor.com/word_vba_examples.htm and
http://www.gmayor.com/SelectFile.htm useful.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
W

WillW

Thanks, Graham -- It took me a while to get back to this, but I can see the
possibility of its working.

A basic problem for me, though: I can't make the Dropdown1 legacy control
function as a dropdown. It just sits there looking grey with no hint of a
dropdown. When I step through the macro, the background color changes
appropriately (matching the word that happens to be displayed), but I'm not
able to select the color from the legacy control. And exiting the control
doesn't change the color of A1.

What am I doing wrong? (Entering "legacy form control" in Word Help is
useless)

Thanks
 
W

WillW

Want to hold my hand through one more step? Everything seems to work in its
parts.

The legacy dropdown is located in cell (1,4) of the first table on the page
(there are others lower on the page). I changed the cell reference in the
macro so the same cell changes color. I stepped through the macro, and it
works. I selected the macro to run on Exit (on exiting the dropdown?).

I protected the form and saved it as a .dotm. But the macro is not
triggered when I move from the control (to verify, I set a break point).
Thinking it might be a macro security issue, I "enabled all macros".

More help please.
 

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