Solved

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

Posted on 2014-04-09
15
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 47

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

734 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