Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
![]()
Jay,
Thanks! This works nicely. Alan |
Reply |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Word 2007 table cell color selection | Tables | |||
Cell entry based on selection in dropdown list | Tables | |||
change color of cell based upon form drop down selection | Tables | |||
Hide a row in a table based upon a previous selection | Tables | |||
Text form field inside WORD table cell, odd selection behavior | Microsoft Word Help |