Solved

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

Posted on 2014-04-09
15
551 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 45

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

757 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

21 Experts available now in Live!

Get 1:1 Help Now