Solved

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

Posted on 2014-04-09
15
555 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 14

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 14

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 14

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
 
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 14

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 14

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 14

Expert Comment

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

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 14

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA, find a string in a column, update a cell 7 64
Add a task in Outlook from access 11 35
Pdf to Mobi 1 59
Recommendation vb6 to vb.net or others 14 105
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

929 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now