|


Today someone (Matt from Georgia) emailed me with a Word form question. Although I also directed him to my free Word VBA/Forms support group (http://groups.yahoo.com/group/Word_VBA), I happened to have a few minutes, so I tossed him back some code and instructions.
He wanted to know how to automate his form so that the user could check one checkbox and have that action also cause other checkboxes on the form to become checked. He apparently was creating a form that allowed people to choose several items. However, if the user wanted to choose all the items on the form, he wanted to provide them with one main checkbox that would save time by causing all the items to be selected.
After sending my reply, I realized my answer would make a good article, as I'm sure there are others out there who would want to know how to handle this same issue. So here's a little lesson in Word autoforms and working with checkboxes.
First you need to design your form. Add your content and insert checkboxes, as needed. Below is an image of my super snazzy form. Okay, so I've designed a few better ones. Please use your imagination to assume this is a great looking form. As you can see in my snazzy form, I have one checkbox serving as the master checkbox. Below that are individual items. The user could just check the individual items they wanted. However, if there were several checkboxes and they wanted to check them all, having this master checkbox feature is a nice option.

Once you've added your checkboxes, from the Forms toolbar displayed by clicking View > Toolbars > Forms, you need to work on your code. Later you'll link the code to the master checkbox.
Note, if you are using Word 2007, you need to revert to the Legacy form fields. Yes, Microsoft wants you to forget all about these useful fields and move on to the new XML Controls. But for now, we'll stick with the old form fields.
For 2007 users, make sure you have displayed the Developer tab, which is not displayed by default. To do this, click Office Button > Word Options > Popular and check Show Developer tab in the Ribbon.

Now move to the Controls group where you'll find a folder containing the Legacy Forms fields, as shown below.

If you are using 2007 and want to learn more about these new 2007 Controls, go to our TechTrax Archives and search CONTROLS for an article explaining the basics on how to use them. (We'll have more articles on them in the near future.)
So your form is designed and, of course, you've already saved it as a template, right? Remember, there are two types of computer users...those who save often and those who wish they did!
Now we need to move into the VBA editor to write our code. You can quickly access it by hitting Alt + F11.
For this article, I'll be putting my code into my NewMacros module, where I keep all my sample article codes.
When you record macros, the NewMacros module is the default location where all those get stuffed. But if you want your code to travel with your template (which, of course, you do want), you will need to insert a module into your template project for this form.
Therefore, find the name of your project template in the list. Click on it to highlight it. Then click Insert > Module, as shown below.
This action will insert a generic named module into your form template. If you want to change the name of the module, select it and make sure your Properties window is displayed (under View). Then you can change the Name property of the selected module, as shown below.
Time for a little code writing. Make sure you have double clicked on the module where you want this code to reside so that it opens the code window. Then start typing in the code below.
Note that if you are new to this process, code must be typed exactly right or you will get errors. The proper way to write code is called its syntax. So be sure that you do not have any extra spaces, commas or returns where they don't belong or you will get a syntax error!
Sub MasterCheckBox()
'declare a variable as boolean, since checkboxes are yes/no, true/false
Dim blnMasterValue As Boolean
'set the value of the variable to the value of the master checkbox
blnMasterValue = ActiveDocument.FormFields("Check1").CheckBox.Value
'if the master variable value is true (checked)
If blnMasterValue = True Then
'then mark these other two named checkboxes as true, also
ActiveDocument.FormFields("Check2").CheckBox.Value = True
ActiveDocument.FormFields("Check3").CheckBox.Value = True
'otherwise (if that variable is not true, i.e., false...not checked)
Else
'then mark these same other two checkboxes as unchecked
ActiveDocument.FormFields("Check2").CheckBox.Value = False
ActiveDocument.FormFields("Check3").CheckBox.Value = False
End If
End Sub
Also, if you are new to coding, make sure that you indent the code as shown above. Not indenting won't cause errors, but indents make your code easier to read. It's much easier to see how things relate to each other when like items are properly indented. But more important, you can avoid errors for missing commands if you indent because it's easier to see if you missed something when things are correctly lined up. You'll be less likely to forget to type the End If ending code if your code is properly indented versus if it was all slammed to the left margin.
The lines of red text are comments. These are incredibly important to add, particularly if you're a coding virgin. Why? Because although you think you will remember why you coded something the way you did a year from now, chances are you won't. Real projects rarely have as few lines of code as does the procedure above. If you have to go back and figure out what you did a year from now...or if someone else has to read it...you'll find that those little notes (comments) you left to yourself will save you a lot of time. You won't look like a deer in headlights trying to figure out why the heck you wrote it the way you did. The reasons will be right there in front of you.
To understand what I've done in the code above, just read the comments!
Now it's time to link your procedure to your form field so you can test it. Hit Alt + Tab to switch over to your form. (But, of course, while still within the editor, you did hit Ctrl + S to save your code, right? I knew you wouldn't forget that important step!)
Double click on your master checkbox form field. This will open the Form Fields Option dialog box. If you decide to change the bookmark name of any of the form fields, which I always do recommend so they are more meaningful than Checkbox1, Checkbox2, be sure that you also change their respective names in your code!
(I actually left mine as Checkbox1. The image below is just to illustrate where you would change it if you did. My code is still correct and will work with Checkbox1 as the master checkbox.)

Click the dropdown for Run macro on Exit and scroll to find the name of your MasterCheckbox procedure (or whatever you called it).

By doing this, you have now told the form field that...when a user hits the Tab key to move out of the checkbox or clicks anywhere else on the page, which moves them out of the checkbox, the MasterCheckbox code should run...run on Exit (when the user exits this form field).
When the code runs, it will decipher the value of the master checkbox and, subsequently, set the value of the other checkboxes that you've added in your code, to also be checked...or vice versa, as the case may be.
To test your template, you first need to lock the form. This activates the form fields and locks away the boilerplate text (form content) from being changed. The user can now only change the form by working within the designated form field areas.
To lock the form for testing, you can just click the lock icon on the Forms toolbar, as shown below.
However, when you're ready to distribute your form, you should use the Tools > Protect Document command and then choose to lock the form, as shown from Word 2003 below.
You might also want to add a Password to the form locking command. If you don't, just click OK on the locking dialog box that will appear. Otherwise, enter your password before you click OK.
In Word 2007, you'll find the Protect Document command on the Developer tab in the Protect group. Click it and choose Restrict Formatting and Editing, as shown below from Word 2007, to display the same protection panel shown above from Word 2003.

Once the form is locked, test it. Click the master checkbox and use the Tab key to move through the form. If you followed my instructions in this article correctly, all the other checkboxes should become checked, too. Uncheck the master, tab through and the others will become unchecked.

If you get an error or it isn't working correctly...you'll have to debug your project to see what you did wrong.
Now understand that the only way to make this all work is that the user needs to exit the master checkbox. If there are no other locations where they need to enter information, they won't have any reason to tab or click elsewhere and nothing will happen. So when designing your form, you'll want to make sure there is other information that needs to be entered. If your checkboxes are the last items on the form, you may want to redesign it so they are on the top so other fields are below, prompting the user to move on through the form...hence they exit that first field so the code will run.
In truth, a better solution would be to add some additional code that would tell the master checkbox...if you are checked, check all the others and then move to [named] form field location on the form. This will cause the cursor to jump over the checkboxes that are already checked, saving the user from needlessly tabbing through the additional checkboxes. That would be the more elegant way to handle it.
You could say something like this:
Selection.GoTo What:=wdGoToBookmark, Name:="Dropdown1"
You'd need to incorporate this code into your procedure. The above line will cause the cursor to jump to the form field with that bookmark name. Yes, I could show you how to do this and where to put the code, but I'll leave that little challenge to you to figure out in the hopes that you use logic to make it work correctly. Remember, if you get stuck, you can jump into my Word VBA group to get help working it out.
Also, if you want more form challenges to try, you'll find several other free articles on Word form creation at this link: http://www.mousetrax.com/techpage.html#autoforms
Have fun!

|