Solved

Automatically formatting form field or cell containing form field in Microsoft Word

Posted on 2014-04-09
15
571 Views
Last Modified: 2014-05-06
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
0
Comment
Question by:progressbusiness
  • 8
  • 2
15 Comments
 
LVL 15

Assisted Solution

by:DrTribos
DrTribos earned 250 total points
ID: 39990701
Ok... content controls are cool... so is VBA

To open the VBA environment press Alt + F11

Hopefully you will see a window called the immediate window at the bottom of the screen.  If not press Ctrl + G

Using VBA you can find out what the text is:
debug.print activedocument.ContentControls(1).Range.Text

Open in new window


You can copy and paste that code into the immediate window...

So, now you know what you have.

Next, content controls have an OnEnter Event... you can start working with this by going to the ThisDocument module (right hand side, in the project explorer)...

More to come
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39990711
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....
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39990715
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

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Accepted Solution

by:
rfportilla earned 250 total points
ID: 39990730
This would be way easier to do in Excel with data validation and conditional formatting.  You don't need VBA.  If you need it to be in word, you can embed an Excel doc into word.  

I attached a sample
Sample.xlsx
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 39990735
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
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40042912
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40042913
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.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40042916
Gah . sorry for grammar. Phone keyboard :-(
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40043989
And now a deficiency of the mobile site is apparent.  I am unable to find the the comment ID.
0
 
LVL 9

Expert Comment

by:rfportilla
ID: 40044492
39990730: non-vba solution
39990701: vba solution (and good walk-through)
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40045804
Rfportilla - thank you. I am still stuck with mobile / cell phone only access to the world!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question