Click here to view pages related to our site, such as Home Page, Site Map, Guestbook and Contact us.        
Google
Web MouseTrax.com

Related Information...

  • Read Dian's beginning AutoForm series of articles: Please Fill Out This Form, published in Computor Companion Magazine.

  • Check out TechTrax Ezine, click Archives and enter VBA to find many more articles.

  • Purchase Dian's AutoForms & Beginning VBA eBook, which includes an online version and eBook.


  • Learn AutoForms in the best possible manner—have Dian show you how it's done through her enhanced video version of the AutoForms & Beginning VBA course.

  Training > Tutorials > Word AutoForms > Creating AutoForms Using Word Basic
 

 

 

 

Creating AutoForms Using Word Basic

by Dian Chapman

This article is for Word versions 6 and 95.
For versions 97 and above, see this article.

Some Basics

One of the coolest things you can do in Word is create Automated Forms. You can create an electronic form that is basically only limited by your imagination and your macro programming skills. Now don't get nervous, you don't have to know how to write macros just to automate a form. But once you find out how cool you can make these forms, just by adding a few easy macros, you'll want to learn how it's done.

The macro language in Word 6 and 7 (95) is called WordBasic (WB). In Word version 97, Microsoft upgraded the programming language to Visual Basic for Applications (VBA). This document was written with WordBasic in mind.

In this document, I will guide you through some basic steps for creating automated forms. It should be enough to get you excited about the possibilities and get you rolling in the right direction. If you need further macro writing help once you've tangled yourself up into a programming knot, you can use your web browser's news reader to access the Microsoft newsgroups at msnews.microsoft.com. (See this Get Help article for more information about accessing the Microsoft newsgroups.) Once you're in, you can find the WB/VBA gurus under the microsoft.public.word6-7macros or microsoft.public.vba groups.

Where do I start?

The first thing you need to do is grab a piece of paper and a pencil. Because that's what you're going to use to sketch out your ideas of what this form should look like. It's really an important step to have a good visual idea before you start. Take the time to think about what it is you're trying to accomplish.what's the natural flow of your form. Will there be any automatic calculations like subtotals or totals? Do you have lists of static (unchanging) items that you can put in a drop down box to help the user make choices? (Note! A drop down box maximum is 25 items.) Will there be any questions that, when answered, will affect where the user should move to in the form?

Example: You can have a check box that asks the user if they are married. If the answer is yes.you jump them over to the "spouse" section of the form so that information can be filled in. If the answer is no.you skip them right to the next question. This will require a little code, but it's not too difficult and I'll show you how to do that later.

After you read this document and learn all the cool things you can do, sit down and start formulating your form in your head or on paper, whatever floats your boat! Once you have a plan of attack, it's time to start molding your page into something really cool.

Organize Yourself

Open a blank page in Word. Now before we begin, let's take a moment to make sure we're all on the same page here. Word gives you three basic page views—Normal, PageLayout and Outline. (Newer versions of Word also include a HTML layout view.) That's what those little buttons are for at the bottom, left corner of your document windows. You can also access these views from the View menu. Start by making sure you're in PageLayout view by clicking View/PageLayout. This is the WYSIWYG (what you see is what you get) view.

Now there are a few more things I'm going to suggest you do. You don't have to, but why make this more difficult than it needs to be by trying to guess what's going on. It's much easier to just have all the information laid out in front of you, right? So click Tools/Options/View. Make sure you turn on Bookmarks. You should also check the Tab Characters and Paragraph Marks. Field Shading should definitely be turned on. You might want to turn them on only when selected. But newbies should make sure they turn them on "always" until you get used to working with fields.

Another big help is Text Boundaries. This will show you where the margins are. Especially when working with form alignment, this will come in handy when you're trying to squoosh a bunch of stuff on one line. Now you'll be able to see where that line ends. Note! If you're using Word 2000 or XP, you should also click Tools/Macro/Security and set the level to Medium. NEVER set it to Low unless you are a development professional who knows how to use the Shift key to prevent auto macros from running viruses on your computer!

Also Note! You can always turn on all your codes quickly by clicking the Show/Hide icon on your Standard Toolbar.it looks like a Paragraph Marker, like a backward 'IP'. Or use the shortcut keys to toggle the Show/Hide markers on/off as needed. Hold down the Ctrl and Shift keys and hit the number 8 key (on the alpha numeric keys, above the letter keys—not on the keypad).

One final item and we'll be on our way. Click View/Toolbars and make sure that your Forms toolbar is turned on. You can drag your toolbar out on to your document screen if you want. This does make it faster to click and insert form fields, drop downs or check boxes as you create your form.

Let's Create a Form

Okay, you're ready. Let's create a form. You have that blank sheet of paper on your monitors in Word. Press enter a couple of times to move down the page. Now either click the Text Form Field button on the Forms toolbar or click Insert/FormField/Text. You should see a shaded box. Now press enter a few more times and insert another Text Form Field. Click Tools/Options/ProtectDocument and choose Forms and click OK. Now look at your page, type some gibberish in the first shaded box, press the tab key and type more gibberish. Check it out! You just created an form in Word!

Cool; but not very exciting, right? However, these are just the basic steps. Just type some text as titles to guide the user through the shaded boxes. Insert a few fields. Protect the page as a form. And finally click File/SaveAs and save the page as a Template. Then you can pass the dot to others who must click File/New to use, then select your form template to create a new document from your master template without destroying any of the text you slaved over.

Messin' Around

Okay, so now you get the general idea. Let's have some fun and mess around with some of the cool features involved in creating forms. Make sure you have an open page on your screen so you have room to add some junk and play around. Note! If you're using the same page we tested above.make sure you click Tools/UnprotectDocument so it will be opened and you can continue working on it.

Text Form Field

Click Insert/FormField/Text or click the first icon (Text) on your Forms Toolbar. You'll see the shadowed square TextFormField. Now there are many options you can specify for this and other fields. To access the Options dialog box, either click Insert/FormField/Option, click the Form Field Options icon from the Forms toolbar, or just make it easy and double click on the Form Field itself to open the Options dialog.

Play around in this dialog box, click drop down arrows—find out what's in there. Notice that you can set limits for the size or type of data a user should enter. This is great to ensure users correctly fill in the requested information. If you ask for a date, be sure to set a date limit. Then if they don't type a date, they'll receive an error and be asked again to type the correct info. You can also set length limits. If you are asking for the name of the state they live in and want that information typed into a database using the two state abbreviation, set the length limit at 2 to ensure they type the data as you require.

You can even add your own Help to your form. You can type in instructions regarding the information being requested in your form. Should the user be unsure what data are needed, remind them to press F1 which will bring up your personal instructions regarding that particular form field and what needs to be typed in this area.

Two very important areas in this dialog box that you should notice are the Bookmark and the Run Macro options. We'll discuss them in more details later. But this is where you'd attach a macro so that it will automatically be activated when you enter/exit the field. And bookmarks are a vital part of forms in Word. Although you are given a default "Text1" bookmark, you really should change those to something more descriptive to save your sanity when you start programming directional macros. It's much easier to type EditGoTo .Destination = "Spouse" than to know that EditGoTo.Destination = "Text24" is the form field where the spouse's name should be typed.

Drop Down Boxes

You can create your own list of possible choices for your form by using a Drop Down Box. If the list of items you want to display is not more than 25, this is a great option. Let's try it. Click the third button on the Forms toolbar, the one that looks like a drop down list. As with Text Form Fields, you can set options by either clicking the Form Fields Option icon, clicking Insert/FormField/Options or just double click on the field. And also, again, you have the ability to add online help text to this field and the option to create a macro to run when the user enters or exits these fields.

Type in a few items in the DropDown Item input box and add them to the list. You can move them around to get them in the best "quick select" order. Then click OK. You won't easily notice that this is a drop down box at first. But once the form is protected as a form and the user hops into this field, a little arrow will appear which visually says "click me." Try it! Add a drop down list to your page. Add in a few items. Click Tools/ProtectDocument. Then press tab to move into the field. (If you only have this drop down box on the page, you might want to add a text field above it so you can see just how it looks when you jump from one field to another.)

Check Boxes

You might like to add a little variety to your form by using a check box rather than just asking someone yes or no text answers. These can also be fun when you combine them with the automation of a macro. You can set it up so that the box is automatically checked depending on the user's answer elsewhere on the form. Or you can use the check box answer to decipher where you should lead the user next, as mentioned above in the "jump to spouse" example.

Fast Forms with Tables

Most forms have specific areas lined up for name, address, etc. By using tables throughout your form, you can make the creation of your form not only fast, but creative. You can add some attractive shading, lines, shadows and text attributes to format your tables. However, be aware that your table cells will tend to jump should the user get carried away filling out the requested information. So if you plan to allow half a line of space to type the user's name, you should also count or estimate the character spaces and limit the text field to that maximum number of spaces to ensure that your cells don't become jumbled.

Boilerplate Text & Sections

If you've ever wondered what Boilerplate Text is, it's just the static (unchanging) text in your form. You'll add text to the page to help inform the user of what information is needed, as well as make sense of the information after you print the form out. If you use table cells to create your form, you might want to put the boilerplate titles in their own formatted column or possibly give them their own Character Style. Then should you need to change the font attributes (say, you decide to make them italic, rather than bold), it will be easier to do, and your form will look consistent.

And remember, text in ALL CAPS IS DIFFICULT TO READ. SO DON'T USE TITLES IN ALL CAPS. IT MAKES THE FORM MORE DIFFICULT TO READ. YOUR EYES AREN'T USED TO THIS STYLE AND YOUR BRAIN WILL BE OFFENDED AND YOU'LL GET A HEADACHE! SEE WHAT I MEAN?

Should there be areas where the user will have free reign to type in lots of information or an area where they may be allowed to edit the text in the document, you should click Insert/SectionBreak and add a break in your form. Not only does this tell Word "and now for something completely different," but it gives you the ability to unlock this section while still protecting other sections. When you're ready to lock your form up for distribution, make sure you click the Option button on the Tools/ProtectDocument dialog box. Uncheck those sections, if any, which don't need to be locked away from the user. Tip: If you do need to add space for the user to elaborate, you might consider adding a page at the end for "Comments." This will mean you won't have to create lots of individual "free" sections in your form, just one big open page at the end.

Utilizing Bookmarks

Bookmarks are your friends! In Word they are great for marking off specific information which can then be selected by using a macro to grab that bookmarked information and do something with it. Each Form Field has a bookmark. If you have your bookmarks turned on, you will see a gray "I-beam" looking mark. You will see the bookmark name when you double click any field or you can jump to it by clicking Edit/Bookmark. Although Word sets numbered default bookmark names, it's vital to your sanity to enter sensible names which will help you move around your form without guess work.

Deciphering Bookmarked Info with Macros

If you need to find out what information a person has typed, you can use a WordBasic function to go get the information. By placing the info into a Variable, you can then take the variable and test it to see what information you now have.

For example, let's say the form has a drop down box which asks the person's knowledge level. They can choose from Beginner, Intermediate, Advanced or Professional. After they click the drop down and select one of the choices, you need to find out what answer they gave so you can jump them to the appropriate section in your form where they can fill in specific information. Create a drop down list with the above four levels and give each drop down field the bookmark name of "Level" followed by the number, such as: Level1, Level2, etc.

Now write a macro by clicking Tools/Macro. Name the macro KnowledgeLevel and click Create to start. The Sub MAIN and End Sub are entered for you. You will now type between them. First you need to set a variable to hold the answer that will be returned by the function we will use to grab the text from the bookmark (huh??).

We'll use the GetFormResult$( ) function to return the text from bookmark and pass it to the vLevel$ variable.

vLevel$ = GetFormResult$("Level")

Note! I start variables with a small "v" to remind me that it is a variable should my code become complicated and this also allows me to use words that might be reserve words. Reserve words shouldn't be used in your own code as variables, such as Print. But a variable like vPrint will be accepted and it's easy to see what it represents. Note, many people use lowercase words for variables and mixed case words for statements/functions.

Now you will test the variable to see what answer is given. You could use an If.Then statement, but because there are four possible choices, a Select Case statement will be more efficient. Pretend that you have specific pages in your form created for each knowledge level of the user. You've added the name of the bookmark in each section. This is where we are going to send the user after we know what the user has answered, as shown below:

(You may wish to add these four bookmarks to surround some text or put them in a field on your page so you can see whether you jump to the correct spot while testing this macro.)

Select Case vLevel$
 Case "Beginning"
   EditGoTo .Destination = "BeginningPage"
 Case "Intermediate"
   EditGoTo .Destination = "IntermediatePage"
 Case "Advanced"
   EditGoTo .Destination = "AdvancedPage"
 Case "Professional"
   EditGoTo .Destination = "ProfessionalPage"
 Case Else
   MsgBox "Houston, we have a problem!"
End Select

Notice that I've also included Case Else and added a message box that will return notification if none of the 4 choices was selected. This could happen if you had a type-o in the drop down list or the macro code. The message box will let you know that you didn't match anything.

Save the macro and add it to the Exit option in this drop down's Form Field Options section. Then once you reprotect the form and run a test, you'll see that after you select a choice from the drop down box, you will press the tab key to try to jump to the next spot in the form (as any good user would), but the macro will be activated when the user tries to move out of this field, the answer will be tested and the macro will shoot the user over to the first form field on the page where they should be.as deciphered by the case statement. Pretty impressive, eh?

You can enhance this code by writing a bit more elaborate statements that would include disabling other fields in the form. Remember when you looked at the Options dialog box for all the form fields, you may have noticed a check box for Enabled. You can write statements in your macro that would toggle that option on the fly (during automation) when a specific answer has been given. Let's say you ask the user whether they have children. If the answer is no, you can have all the form fields related to questions about children disabled so the user will automatically pass over them and they will appear grayed out on the form. Or maybe start with them disabled and you can reactivate them if they do have kids. Makes your form look very intelligent!

This same process can be used with check boxes. By using the function GetFormResult( ), the function will return a number and put it in your integer (number) variable. If the answer is a 1, the box is selected. A zero would mean it's not selected.

Note! There are two types of variables in WordBasic.Integer and String. Integers return numbers and Strings return text. In WordBasic these two are differentiated by whether there is a $ (dollar sign) after the item. No $ is an integer, having a $ tells you it's a string. Therefore, GetFormResult( ) will return an Integer (number).and GetFormResult$() will return a String (text). Mix them up, and you'll get a "mix matched" error.

Distributing Your Template

You've thought your form through.you've created your form.you've tested your form and now it's time to knock the socks off your colleagues by having them use your form. Now what?

After you've created all the boilerplate text and have inserted all the form fields, as well as written all the macros that help operate your form, you must click Tools/ProtectDocument/Forms to lock up your form so users can't mess with the text and so the form fields will automate and tabs will jump from field to field. You would have been locking and unlocking the form while your were testing it, but now you might want to even add a password so no one can unlock the original if you're passing it to a network drive for all to use.

Then you must save the document as a Template. Click File/Template and give it a name. It must be a template so users can click File/New and create a new doc from your dot (template). This way your form can be used over and over again.

As you were creating macros for your form, they all should have automatically been saved with this template. However, if you wrote any macros before you bothered to save this template.they may have defaulted to your Normal.dot (master template). In that case, you'll have to make sure all the necessary macros are attached. Click Tools/Macro and check for all your macros. If any are missing.click Organizer and see if they're in your Normal.dot and pass them back to your Form template.

Some Closing Thoughts

So.are you excited yet? There are many other topics that could be covered when discussing forms, but I just don't have the space on this page. Quickly, however, let me add a few suggestions.

  • You can add macros that will include calculations so that a field can automatically total up a bunch of numbers for you—great for invoices.


    However, since the form is locked, you'll have to write a macro to do the calculations and start the macro with a the ToolsUnprotectDocument statement to temporarily open the form so the calculations can be added. When you relock it with the ToolsProtectDocument argument, don't forget to add the .NoReset = 1, .Type = 2 arguments to ensure that the fields which have already been filled out are not cleared when the form relocks.

  • You can set a variable to hold an answer to a field and have that answer passed and automatically inserted into many other places, this will save the user typing time by filling in duplicate information for them automatically.

  • You can use message boxes to ask questions in your form. Have a message box pop up from an Entry macro that asks whether the user wants to print the form. With Yes/No buttons added to the message box, you can decipher which button was clicked and act on the answer, such as printing the document or just saving the document.

  • Message boxes are also great for informing the user that there is a problem. Say all forms must have an ID number entered. Should the user try to skip that field, a message box could pop up on exit, if you've deciphered the ID number is not there, inform the user that the form cannot be completed without entering an ID number.

  • And remember, message boxes are great for troubleshooting macros. If you're having problems or not getting the correct answer, let a message box show you the variable that is being passed so you can see if it's the correct information.

  • You can really get fancy and use the Dialog Editor to create your own custom dialog boxes with check boxes, radio option buttons and/or input boxes. This will allow you to offer many answers to the user. Once completed, the dialog box can pass gathered information to various places in the form. Less work for the user (but more code for you)!

  • And should you have a preprinted form that you want to overlay with your own online fill in page, you can scan the form and insert it on your page as a picture. Wrap this form picture in a frame and lock it to the page. Then select the form picture and click Insert/Frame again. This time draw a new frame over the form picture. Now add your form fields in this frame. But be warned, you have to be a pretty good "jiggler" to wiggle all those fields into the exact places so they print out properly. When the user prints the form, make sure to set the Print Data Only option in the Tools/Option/Print dialog box.

  • You can write a final macro using the MacroCopy statement that will pass your original macros from this template to a duplicate copy of the form. Then add the -1 argument and your macros will be locked up so no one can change them. However, make sure you keep the originals open in your original template so you can make changes if necessary, because you will not be able to reenter the code once it's passed to a locked version!

I hope this information has made the learning easier for you. Remember to check out all my other resources for learning AutoForms & VBA.


Go to Top