I hadn't realised that this was a form field when I posted my reply. I had
assumed a numeric Word or merge field.
I posted the following in response to a similar query a while back - no-one
did come back with the simpler version that worked.
"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."
I have modified the macros originally posted to eliminate a minor error and
to improve the error trapping. The result is as follows:
Set the form field (here "Text1") to be a text field with unlimited length.
The original macr0 used the format (000)-000-0000 You don't appear to want
the brackets, which makes the macro a little simpler so:
Sub FormatNum()
'run on exit from the phone mumber field
Dim sPhone As String
Dim sNum As String
sNum = ActiveDocument.FormFields("Text1").Result
If Len(sNum) = 10 Then
If IsNumeric(sNum) = False Then GoTo WrongNo
End If
If Len(sNum) <> 10 Then
If Len(sNum) <> 12 Then GoTo WrongNo
If InStr(1, sNum, "-") <> 4 Then GoTo WrongNo
If InStr(5, sNum, "-") <> 8 Then GoTo WrongNo
End If
sPhone = format(sNum, "000-000-0000")
ActiveDocument.FormFields("Text1").Result = sPhone
Exit Sub
WrongNo:
MsgBox "Incorrect telephone number format." & vbCr & _
"Insert 10 digits without spaces" & vbCr & _
"For 123-456-7890 enter 1234567890", vbCritical, _
"Number entry error"
End Sub
Sub FormatError()
'run on entry to the field after the phone number field
Dim sNum As String
sNum = ActiveDocument.FormFields("Text1").Result
If Len(sNum) <> 12 _
Or InStr(1, sNum, "-") <> 4 _
Or InStr(5, sNum, "-") <> 8 Then
ActiveDocument.FormFields("Text1").Select
End If
End Sub
http://www.gmayor.com/installing_macro.htm
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>