Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
Alan[_6_] Alan[_6_] is offline
external usenet poster
 
Posts: 23
Default Setting color of table cell based on list selection

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


  #2   Report Post  
Posted to microsoft.public.word.docmanagement
PamC via OfficeKB.com PamC via OfficeKB.com is offline
external usenet poster
 
Posts: 582
Default Setting color of table cell based on list selection

AFIK, there is no built-in way to do this. Have you considered using Excel?

PamC



Alan wrote:
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


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...ement/200903/1

  #3   Report Post  
Posted to microsoft.public.word.docmanagement
Alan[_6_] Alan[_6_] is offline
external usenet poster
 
Posts: 23
Default Setting color of table cell based on list selection

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
  #4   Report Post  
Posted to microsoft.public.word.docmanagement
Alan[_6_] Alan[_6_] is offline
external usenet poster
 
Posts: 23
Default Setting color of table cell based on list selection

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
  #5   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Setting color of table cell based on list selection

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

On Sat, 28 Mar 2009 19:41:10 -0700 (PDT), Alan wrote:

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



  #6   Report Post  
Posted to microsoft.public.word.docmanagement
Alan[_6_] Alan[_6_] is offline
external usenet poster
 
Posts: 23
Default Setting color of table cell based on list selection

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
  #7   Report Post  
Posted to microsoft.public.word.docmanagement
Alan[_6_] Alan[_6_] is offline
external usenet poster
 
Posts: 23
Default Setting color of table cell based on list selection

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

  #8   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Setting color of table cell based on list selection

Alan wrote:
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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


  #9   Report Post  
Posted to microsoft.public.word.docmanagement
Alan[_6_] Alan[_6_] is offline
external usenet poster
 
Posts: 23
Default Setting color of table cell based on list selection

Jay,
Thanks! This works nicely. Alan
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2007 table cell color selection Dick De Vries Tables 7 May 16th 07 04:22 PM
Cell entry based on selection in dropdown list jcachat Tables 2 November 12th 06 02:53 PM
change color of cell based upon form drop down selection Tim Doyle Tables 1 October 27th 06 07:41 PM
Hide a row in a table based upon a previous selection [email protected] Tables 6 June 7th 06 02:46 PM
Text form field inside WORD table cell, odd selection behavior pwrichcreek Microsoft Word Help 1 October 11th 05 03:57 PM


All times are GMT +1. The time now is 03:55 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"