Link to home
Start Free TrialLog in
Avatar of progressbusiness
progressbusinessFlag for Australia

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
SOLUTION
Avatar of DrTribos
DrTribos
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Next... go to the "ThisDocument" - it's actually in a folder called "Microsoft Word Objects".  Double click on the "ThisDocument"

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_ContentControlOnExit".  Some text should then appear in the window
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
End Sub

Open in new window


More to come....
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

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And here is a picture showing where everything lives...

Note about the code:
By declaring the variable like this
Dim clCol As WdColor

Open in new window

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

Open in new window

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
Avatar of Martin Liss
I've requested that this question be deleted for the following reason:

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.
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)
Rfportilla - thank you. I am still stuck with mobile / cell phone only access to the world!