Formatting a Form Field (Telephone Number)

L

Lilbit

Is it possible that when a user lands on a form field, that form field wil
show ( )___-____. When they go to type in the telepone number, the 1s
digit will appear after the open parenthesis; after typing in the thir
digit, it will jump to the 1st underlined space; after typing in thre
numbers, it will jump to the 1st underlined space after the dash so the
can type in the last four numbers? Thanks!

-
Message posted using http://www.talkaboutsoftware.com/group/microsoft.public.word.docmanagement
More information at http://www.talkaboutsoftware.com/faq.htm
 
S

Suzanne S. Barnhill

I can't get this to work with parentheses, which apparently can't be
included in a "number format," but if you set the form field type to
"Number," set 12 as the maximum number of characters, and then use
000-000-0000 as the number format (checking the box for "Calculate on
exit"), users can enter numbers continuously, and when they tab out of the
field the result will be formatted as 123-456-7890. I can't help feeling
there must be some way to accomplish this with the format you want, but I
haven't been able to achieve it. It would appear not to be possible to add
formatting switches to FORMTEXT fields, so that's no help, either.
 
G

Graham Mayor

Someone is going to come up with a ridiculously simple approach that works,
but in the meantime you can do it with two macros. The first run on exit
from the telephone number form field (here Text1) and the second run on
entry to the following field. The bulk of the macros is concerned with error
trapping. The macros should allow phone numbers to be entered as 1234567890
or (123)-456-7890 but most other formats should be trapped.

Sub FormatNum()
Dim sPhone As String
Dim sNum As String
sNum = ActiveDocument.FormFields("Text1").Result
If Len(sNum) <> 10 Then
If Len(sNum) <> 10 Then
If Len(sNum) <> 14 And InStr(1, sNum, "(") <> 1 Then
MsgBox "Incorrect telephone number format." & vbCr & _
"Insert 10 digits without spaces" & vbCr & _
"For (123)-456-7890 enter 1234567890", vbCritical, _
"Number entry error"
Exit Sub
End If
End If
End If
sPhone = Format(sNum, "(000)-000-0000")
ActiveDocument.FormFields("Text1").Result = sPhone
End Sub

Sub FormatError()
Dim sNum As String
sNum = ActiveDocument.FormFields("Text1").Result
If Len(sNum) <> 10 Then
If Len(sNum) <> 14 And InStr(1, sNum, "(") <> 1 Then
ActiveDocument.FormFields("Text1").Select
End If
End If

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top