How do I validate a phone number in a Word 2003 form?
Hi,
I have a form in Word 2003 with several phone number fields. I am trying to run a Macro that will check each one of these fields to ensure that the user has entered a numeric value in the format xxx-xxx-xxxx.
Also, is there a way I can create a loop that will check each phone number field in turn instead of running a separate Macro for each field?
Any help will be greatly appreciated!
Thank you
Microsoft WordVisual Basic Classic
Last Comment
B3ans
8/22/2022 - Mon
lwebber
How did you create the phone number fields? Did you use Word's Form Field feature? Which version of Word are you using?
B3ans
ASKER
I'm using Word 2003. I created the phone number fields using the form field feature and set the properties to accept only numbers
lwebber
You can loop through all form fields in the document with code like this:
Sub Test()
Dim thisFormField as FormField
For each thisFormField in ActiveDocument.FormFields
Debug.Print thisFormField.Name & ": " & thisFormField.Result
Next
End Sub
In place of the "Debug.Print" you would perform a check for the correct phone number format. But before we get into that, note that Word doesn't know which form fields are supposed to be phone numbers and which are for other purposes. Unless the *only* form fields in your document are for phone numbers, then you will have to mark the phone number fields in some way. The best way is to give the phone number form fields names that start with "Phone". E.g. "Phone 1", "Phone 2", or "Phone Applicant Home", "Phone Applicant Cell", etc.
BTW, the form field's name is the Bookmark name you assign in the form field properties dialog.
B3ans
ASKER
This is the code I'm using:
Sub PhoneCheck()
Dim thisFormField As FormField
For Each thisFormField In ActiveDocument.FormFields
If Len(.Result) < 10 Then
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoPhone"
MsgBox "Invalid Entry. Phone number must be 10 digits"
End If
Next
End Sub
Sub GoBacktoPhone()
What goes here?
End Sub
I'm not able to figure out what goes in the GoBacktoPhone() macro. I want it to go back to the same form field if the entry is invalid.
Thanks a lot for your help!