Setting color of table cell based on list selection

  • Thread starter Thread starter Alan
  • Start date Start date
A

Alan

I have a table. One column contains cells with a drop-down list
of a couple of values. Is there an easy way to set the color of that
cell based on what the user selects in the list?

I could not find a way to do this, looking at Word help or from
a Google search.

I am using Word 2007. Thanks, Alan
 
This code should do it, but I am having problems referring to the drop-
down list I inserted into the table (display shows "Risk Level", tag =
"Risk".

Sub ColorRiskCell()
With ActiveDocument
If .FormFields("Risk").DropDown.Value = "High" Then
ActiveDocument.Tables(1).Cell(3,
3).Shading.BackgroundPatternColor = wdColorRed
Else
ActiveDocument.Tables(1).Cell(3,
3).Shading.BackgroundPatternColor = wdColorGreen
End If
End With
End Sub

So, I tried to print out the names of all the form fields in the
document, but got no output:

Sub CheckFormFields()
Dim aField As FormField
For Each aField In ActiveDocument.FormFields
Debug.Print "Form Field Name: " & aField.Name
Debug.Print "Form Field Type: " & aField.Type
Next aField
End Sub

I cannot use Excel, because this is a form in Word.

So, I am confused. --- Alan
 
OK. I now know how to change the color (below), but I do not know how
to detect that the selection changed.

Alan

Sub ChangeRiskColor()
Dim myCells As Range
With ActiveDocument
Set myCells = .Range(Start:=.Tables(1).Cell(3, 3).Range.Start,
_
End:=.Tables(1).Cell(3, 3).Range.End)
myCells.Select
End With
With Selection
If InStr(.Text, "Low") Then
.Shading.BackgroundPatternColor = wdColorGreen
Else
If InStr(.Text, "High") Then
.Shading.BackgroundPatternColor = wdColorRed
End If
End If
End With
End Sub
 
Your reference to a tag and the lack of results from the CheckFormFields macro
indicate that the "drop down fields" you used are actually content controls and
not formfields. That's why there are no members of the FormFields collection.

Either you can replace those content controls with dropdown formfields from the
Legacy Tools button (fourth from the left in the second row of the Controls
group), or you can change the macro to work with the content controls.

You can't refer to a content control directly by its tag the way you could refer
to a formfield by its bookmark name; instead, you must loop through the
collection until you find one with the desired tag (and note that tags aren't
necessarily unique). The modified code would be

Sub ColorRiskCell()
Dim oCC As ContentControl

With ActiveDocument
For Each oCC In .ContentControls
If oCC.Tag = "Risk" Then Exit For
Next

If Not oCC Is Nothing Then
If oCC.Range.Text = "High" Then
.Tables(1).Cell(3, 3).Shading.BackgroundPatternColor _
= wdColorRed
Else
.Tables(1).Cell(3, 3).Shading.BackgroundPatternColor _
= wdColorGreen
End If
End If
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
Jay,
Thank you! I guess that content controls are "new and
improved" and formfields are "old" in 2007.

How do I trigger off the change in a FormField? How do I
trigger off the change in a content control?

Alan
 
To be clear, all I want to do is have a drop-down list in a table
cell, automatically change the color of the table cell based on the
selection, and have it print out without any drop-down list graphics.

Content controls seem to fit the bill, if I can determine how
to trigger off the change in them.

Thanks, Alan
 
Alan said:
To be clear, all I want to do is have a drop-down list in a table
cell, automatically change the color of the table cell based on the
selection, and have it print out without any drop-down list graphics.

Content controls seem to fit the bill, if I can determine how
to trigger off the change in them.

Thanks, Alan

Instead of the macro in the previous reply, put this one into the
ThisDocument module of the template:

Private Sub Document_ContentControlOnExit( _
ByVal ContentControl As ContentControl, _
Cancel As Boolean)
With ContentControl
If .Tag = "Risk" Then
If .Range.Text = "High" Then
.Range.Cells(1).Shading.BackgroundPatternColor _
= wdColorRed
Else
.Range.Cells(1).Shading.BackgroundPatternColor _
= wdColorLightGreen
End If
End If
End With
End Sub

This is an "event handler", which Word automatically calls when the cursor
moves out of any content control. The parameter "ByVal ContentControl" tells
the macro which content control the cursor moved out of. (It should be noted
that the event handler isn't called immediately when the dropdown is
changed, only when the cursor leaves the control.)

The first If statement determines whether the current content control is the
one whose tag is "Risk". If it is, then the value of the dropdown is tested
and the color is set accordingly.

In this version, instead of coloring the cell at a specific row and column
number, it colors whatever cell contains the content control -- that is, the
first (and only) cell in the range of the content control. That way, if rows
or columns are added or removed, the macro will still color the correct
cell.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
I'm really new to Visual Basic and have only started to learn over the past couple of days. I am trying to achieve similar results as Alan. I have a table with one column that contains a cell with a dropdown form field that contains 4 values. I want each value that is selected from the list by the user to change the cell colour based on the RGB colour scheme.

I noticed this post but wasn't sure if it would help me as I only have Word 2000. Is this possible and is there also a way of making it compatible between the different editions of Word?

Thanks Matt.
 
Back
Top