[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2014-04-09
15
Medium Priority
?
597 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Accepted Solution

by:
rfportilla earned 1000 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 49

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
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…
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…
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 …
Suggested Courses

650 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