This article is the fourth in a series on
creating Auto Forms in Word. In Part 1, I showed you how to
create a form that lets users enter information into
fields. Part 2 ventured into
the VBE (Visual Basic Editor). I showed you how to write a little code to direct your form users to
specific questions. Part 3 showed you how to create message boxes to provide or
request information which then could be retrieved and used to modify a document.
This article concentrates on creating custom dialog boxes. Although, MsgBox and
InputBox are useful tools, having them popping up all
over the place gets annoying. With a custom dialog box, you can ask more specific questions
with fewer boxes. You can then take that
information and populate areas of a form and add more
obscure information which the user may not know, such as personnel, accounting, or product
codes. This way, the user only has to make simple choices, such as requesting copy paper. The
form then can automatically add in specific
requisition numbers, which make the form more useful.
1 Design your form
When you're going to get involved in
more complex form development, you need to make sure you take time to think about your form
design. Talk to those people who will use the
form. In this expenditure form, I need to include the
manager and department codes, for example.
2 Secure Information
In previous forms, you had to protect the template in order to activate the form fields.
However, this time I don't want the user to be able
to modify certain information, such as department codes. I still use form fields for personal
information, but I also use a single bookmark as a
place holder for department information. This way,
I know where to put the information, but it's locked into the form's boilerplate text so
users can't alter it. To slip the text into the boilerplate,
I do need to unlock the form and then immediately relock it. This protection toggle procedure
deciphers whether the form is locked or not. I then
use this procedure to open the form, add the
information and relock the form.
You can see I've SET an object reference to the ActiveDocument (just because I'm too lazy
to keep typing ActiveDocument). Then I ask if the Doc is protected or not and toggle the settings.
I also added the NoReset argument so my form fields won't be blanked out, as they would be
if you set Protection on a form manually. If you have sections or passwords in your form,
you'd need to handle these arguments as well. How
do you know what to add? Use VBA Help (press the F1 key while you are in the VBE).
3 Create Custom User Forms
When you click Insert|UserForm in the VBE, a blank UserForm appears. A UserForm
is actually a dialog box you build yourself. Each UserForm has Properties you set to design how
it looks. When you click the form, a Toolbox that holds your Controls appears. You use
these ActiveX Controls to build your custom form.
Just click and drag them onto the form. You click
the controls to see their properties and customize
their appearance. Each control has a default name
which you can change (just like the bookmark names you
set with Form Fields).
4 Make your form work
In VBA, the code runs when an event occurs. I double-clicked the OK control on
my UserForm and entered the code below into the Click Event procedure for the button. It tells
the form that when the user clicks the OK button
on this form, set variables to capture the text
typed into the text boxes, then set the results of
form fields using the appropriate bookmark name,
with the information held in each variable. Where
I only use a bookmark, not a form field, I have to call the protect toggle
procedure (pToggleProtectDoc) to open the form before
the bookmark text is entered. To get this UserForm
to appear when you open the document, you can write a simple AutoNew macro that calls the
form with UserForm1.Show. You should also write code
under the Cancel button Click Event, which hides
the form and protects the document against modification.
5 Make design choices
This form asks the user to click the name of his/her department. I used Option Buttons, so
the user can only make one choice. If this form were
asking a question that allowed multiple choices, I
would have used the CheckBox control instead. Or I
could have saved room on the form by populating a
ListBox control with the department choices. Some
controls are easier to use than others, so your design
choices may be limited by your skill until you learn more.
6 Make Programming Choices
The code below runs when the user clicks the OK button. Normally, you would write a more
efficient Case Statement to capture answers from
multiple choices. A Case Statement would look at
the one answer and do something for whatever Case matches the answer. Since you're more familiar
now with If/Then statements, the code I used
goes through the value of each option control and asks
if it's value is True (selected). Then it not only sets
that department's name on the form, but also adds
the appropriate Unit Manager's name and department code for
accounting purposes. Afterwards, the form is
reprotected, unloaded and the next form is called.
For more information on forms, be sure to check out all of Dian's articles
at this link http://www.mousetrax.com/techpage.html#autoforms.