Logo: TechTrax...brought to you by MouseTrax Computing Solutions

Using VBA to Control the Flow of Word AutoForms

by Dian Chapman, MVP, MOS
Skill rating level 6.

You can save yourself a lot of time by using Word AutoForm capabilities to create automated documents that allow the user to quickly fill in necessary information to complete forms, such as applications, purchase orders or invoices. This article will concentrate on controlling the Tab Order flow of a Word form using VBA (Visual Basic for Applications).

Tab Order Problem in Word
Imagine you've created a form that uses a table to align the text form fields. On the left side, you have a column of fields for the client's name and address details. On the right side of the table, you have more columns where a user can fill in different shipping information, if necessary.

Unfortunately, Word's default navigation order, or Tab Order, for tables and forms is from left to right, then down and left to right again. If you were to tab through the field in a form similar to the sample above, you'd notice that you would have to fill out the name for the client at the Installation Location, then the name for the Billing Information. Then back to the Installation Company and next the Billing Company name. That's obviously not the way you'd want it to flow. It makes more sense for the cursor to move down the first column. Then move to the check box to allow the option of checking it if the information for services and billing are the same. If the check box is checked, meaning the Billing Information is the same as the information already supplied, you'll want the cursor to skip the Billing column and get on with the rest of the form.

Controlling the Flow
By using Visual Basic for Applications (VBA) you can write a Select Case statement that will allow you to control where the cursor moves when the user hits the tab key to jump to the next field.

To follow along with this example, you can create a sample AutoForm by placing a table on a page similar to the layout in the image above. Add Text form fields, from the Forms Toolbar (click View/Toolbars/Forms), into cells in two columns. Also add a CheckBox above the table, as in the image, to provide an option whether the billing information should be the same as the client info. Finally, add one last text form field after the table and name it bkDone. Click Tools/ProtectDocument/Forms to lock the form. Save this sample form as a template. You can see the problem by tabbing through these fields as they are now. To fix the problem, we'll write a chunk of code that will adjust the navigation. Hit Alt/F11 to enter the Visual Basic Editor (VBE).

VBE
When you open the VBE, you'll probably be sitting inside a code module for Normal.dot. Most likely you'll be in the New Macros module, which is the default macro code module for your default template. This is not where you want to be. We need to add a code module to the invoice so all the code we write will travel with the invoice.

Highlight your template project and click Insert/Module. An empty code module will be inserted. This is where we'll be writing all the code for this form.

Your Project Files in the VBE should look similar to the image above.

Code
In the VBE code window, we'll start writing our code. We first need to give the procedure a name. Note: I like to use pro as my procedure naming convention, so that when I'm later working in tons of code, calling procedures, I'll know that this is a procedure, versus a function. I use func as my function naming convention. Then we need to name a string variable to hold the name of whatever current bookmark we're working within. From there, we'll write a Select Case statement to compare the current variable (Text form field bookmark name) with all possible bookmark names in the document. When it finds a match, it'll tell the cursor to go wherever we direct it, as per the instructions we'll give it in that next line of code.

Add the following code, exactly as I've written it, line for line, except change the bookmark names I have in quotes to the bookmark names you used in each of your form fields. Note that the red, italic text is not code, but comments. You can add these comment into your code to help you remember what action each statement takes. Remember to start each comment line with an apostrophe.

Sub proFieldNav()

'declare a variable to hold the field bookmark name
Dim strCurrBookmark as String

'decipher what type of form field you're currently using
If Selection.FormFields.Count = 1 Then
     'if field is not a textbox but a check- or listbox, capture that name
     strCurrBookmark = Selection.FormFields(1).Name
'if selection is a text field, capture that name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
     'add whatever the bookmark name is, to the designated variable
     strCurrBookmark = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If

Select Case strCurrBookmark
    'if the cursor is currently located in the field that contains the
    'bookmark name in the "case" statement, the lines below it will
    'be enacted, thereby jumping to the field you've indicated.

    Case "bkInstallName"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkInstallCo"
    Case "bkInstallCo"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkInstallAdd01"
    Case "bkInstallAdd01"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkInstallAdd02"
    Case "bkInstallAdd02"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkInstallCityInfo"
    Case "bkInstallCityInfo"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkInstallPhone"
    Case "bkInstallPhone"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkInstallEmail"
    'after the "email" field in first column, we jump up to checkbox option to
    'allow the user to select if if the billing and shipping info are the same    

    'when the user tabs out of the checkbox field, the If...Then statement below
    'deciphers the status of the checkbox and take appropriate action.

    Case "bkInstallEmail"
        Selection.GoTo What:=wdGoToBookmark, Name:="ckSameAs"
    Case "ckSameAs"
        'if the cursor is in the SameAs checkbox, we evaluate if the value is true (checked)
        If ActiveDocument.FormFields("ckSameAs").CheckBox.Value = True Then
             'if checked, jump down to the next (Done) field, which is after all the billing fields
             Selection.GoTo What:=wdGoToBookmark, Name:="bkDone"
        Else
              'else, if not checked, go to Billing info to enter data
              Selection.GoTo What:=wdGoToBookmark, Name:="bkBillName"
        End If
    Case "bkBillName"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkBillCo"
    Case "bkBillCo"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkBillAdd01"
    Case "bkBillAdd01"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkBillAdd02"
    Case "bkBillAdd02"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkBillCityInfo"
    Case "bkBillCityInfo"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkBillPhone"
    Case "bkBillPhone"
        Selection.GoTo What:=wdGoToBookmark, Name:="bkBillEmail"
End Select

End Sub

The Exit Macro
To finish the process, you need to enter this macro into each of the form fields you want to examine. Enter the macro name into the form field's option dialog Run Macro on Exit input box. Then when you tab out of each field, this macro will run, thereby checking the current bookmark name and going to it's prescribed new location, by matching the name in the case statement and following the instructions we gave it after each case.

To insert the macro into the field options, double click each form field to open the property dialog box. Add the name of the macro you just wrote into the Exit input box, under Run Macro on. Once you have it entered into all the necessary form fields, save your template. Lock it (Tools/ProtectDocument/Forms) and give it a test.

If you coded it correctly, the cursor should cycle through the field in a more sensible way. First it'll move down the first column. Then the cursor will jump to the SameAs CheckBox, if it's selected, it skips over the billing Information and jumps to the Done field. If it's unchecked, it jumps into the billing Information for you to fill in those fields. Congrats!


Note: If you're interested in learning more about creating Word AutoForms, I've written a series of articles on the subject. My Please Fill Out This Form series takes beginners from the basics, through to database connectivity. You can find this series, along with a other AutoForm articles and VBA code snippets on my web site's TechPage. Go to www.mousetrax.com. Click the TechPage navigation button on my site. There you'll find several tutorials. Click the page navigation link to jump down to the Word AutoForm articles. Or you can jump there directly by clicking this link: http://www.mousetrax.com/techpage.html#autoforms.

And, although the many articles you'll find above, related to AutoForms, will teach you a lot, if you'd like more complete training to learn Word AutoForms and/or Beginning VBA, I also teach inexpensive online classes on AutoForms and other advanced Word uses. See the Training Courses link above for details and enrollment.

 

 

Go up to the top of this page.
This site powered by the Logical Web Publisher™: Content management by Logical Expressions, Inc.