progressbusiness
asked on
Automatically formatting form field or cell containing form field in Microsoft Word
A colleague of mine is trying to create a form in Word which contains a drop down form field. We would like the table cell where the form field sits to change colour when you select an option (e.g. Green, Red, Orange).
I thought perhaps we could do this with Visual Basic but have absolutely no idea where to start.
I've attached a copy of an example of what we're trying to work with.
Form-Field-Colour-Cell.docx
I thought perhaps we could do this with Visual Basic but have absolutely no idea where to start.
I've attached a copy of an example of what we're trying to work with.
Form-Field-Colour-Cell.docx
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Now you get to add your code... the following will work on a simple document but will not be suitable for a more complex document.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim clCol As WdColor
Select Case ContentControl.Range.Text
Case "Green"
clCol = wdColorGreen
Case Else
clCol = wdColorBlue
End Select
ContentControl.Range.Cells(1).Shading.ForegroundPatternColor = clCol
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And here is a picture showing where everything lives...
Note about the code:
By declaring the variable like this
You will have to save the file with a docm / dotm extension to make it macro enabled. You may also have to change some settings in the trust centre to get it working on your colleagues computer (google will tell you how).
Hope this helps
EE-oCC-CellColorChange.jpg
Note about the code:
By declaring the variable like this
Dim clCol As WdColor
we tell word that we are using a color, this is good because you can make use of intellisense to pick colors to match the displayed text. The content control is delared automatically
ByVal ContentControl As ContentControl
and we are using an Event that fires when-ever you exit the content control. You will have to save the file with a docm / dotm extension to make it macro enabled. You may also have to change some settings in the trust centre to get it working on your colleagues computer (google will tell you how).
Hope this helps
EE-oCC-CellColorChange.jpg
I've requested that this question be deleted for the following reason:
Not enough information to confirm an answer.
Not enough information to confirm an answer.
My last post contains a working solution provide a great solution and was provided within 1.5 hrs. It includes: image demonstrating the code, the working solution, and detailed instructions on how to implement it. It also includes explanation of how the code works.
There is certainly enough information. On the other hand the asker has never returned to the question. I object.
There is certainly enough information. On the other hand the asker has never returned to the question. I object.
Gah . sorry for grammar. Phone keyboard :-(
And now a deficiency of the mobile site is apparent. I am unable to find the the comment ID.
39990730: non-vba solution
39990701: vba solution (and good walk-through)
39990701: vba solution (and good walk-through)
Rfportilla - thank you. I am still stuck with mobile / cell phone only access to the world!
At the top of the large window you should see 2 dropdown menus, the first probably says "(General)". Click this and select "Document"
Then on the other dropdown select "Document_ContentControlOn
Open in new window
More to come....