How to validate the contents of a formfield in a Word form

Article contributed by Dave Rado

By right-clicking on a text form field and selecting Properties, you can set the Type and Format boxes to provide validation. If the user then enters an invalid entry in the formfield they will be intercepted when they try to leave it:

(I'm not sure what Current Date and Current time are doing there – you could use ordinary fields to produce those; but the rest are useful).

However, if you want to perform some validation that is not catered for by the above dialog, you will have to use a macro, assigned as the Exit macro for the appropriate formfield using the above dialog.

Unfortunately, the following macro doesn't work - the macro runs too quickly, and as a result, although the cursor does return to the original formfield for an instant, it then jumps to the formfield you tabbed to or clicked in:

Sub ExitText2()
    With ActiveDocument.FormFields("Text2")
        If Len(.Result) > 0 And Left$(.Result, 3) <> "KLM" Then
            MsgBox "The first three letters must be 'KLM', in uppercase", vbInformation
        End If
    End With
    ActiveDocument.Bookmarks("Text2").Range.Fields(1).Result.Select
End Sub

Fortunately, you can get round this timing bug (as indeed you can get around most of Word VBA's timing bugs) by using Application.Ontime. The following pair of macros working together do work

Sub ExitText2()
    With ActiveDocument.FormFields("Text2")
        If Len(.Result) > 0 And Left$(.Result, 3) <> "KLM" Then
            Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText2"
            MsgBox "The first three letters must be 'KLM'"
        End If
    End With
End Sub


Sub GoBacktoText2()
    ActiveDocument.Bookmarks("Text2").Range.Fields(1).Result.Select
End Sub

Even this looks a little clunky on screen, because you can see the the cursor go to the next formfield before jumping back to the previous one (and Application.ScreenUpdating = False doesn't help; you can fix this using the LockWindowUpdate API, but that is beyond the scope of this article). For this and other reasons, a UserForm (Microsoft-speak for a custom dialog box) is usually a much more elegant solution for form filling than a document-based form. The UserForm code could fill in the document form (with or without using form fields in the document); and validation of UserForm controls is very easy to set up.

Incidentally, the reasons for using the convoluted syntax:

ActiveDocument.Bookmarks("Text2").Range.Fields(1).Result.Select

are discussed in the article: The best way to select a form field using VBA.