Number Formatting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to format a 10 digit number so that it appears as a telephone number i.e. (815) 123-4567 ?
 
Hi there,

Yes.

But you will have to give us more details.Where is the number in relation to
your document? How is entered into the document? Do you want the formatting
done after the fact (i.e. when saving the document, or after) or on the
fly?... What Word Version?

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


jgetch said:
Is there a way to format a 10 digit number so that it appears as a
telephone number i.e. (815) 123-4567 ?
 
Hi

You don't where or in what process you want to do this,
but if you are in a document you can position the
Insertion Point at the start of the number and use the
following macro:

Dim OldS, NewS As String
Selection.MoveRight Unit:=wdCharacter, Count:=10,
Extend:=wdExtend
OldS = Selection.Text
Selection.Cut
NewS = "(" + Left(OldS, 3) + ") " + Mid(OldS, 4, 3)
+ "-" + Right(OldS, 4)
Selection.Text = NewS
Selection.Copy
Selection.Paste

John
-----Original Message-----
Is there a way to format a 10 digit number so that it
appears as a telephone number i.e. (815) 123-4567 ?
 
Hi there,

If you are using a protected form, then use this macro as the On exit event
for the field containing the telephone number:

'_______________________________________
Sub TelNumber()

Dim TelRange As Range
Dim FirstNum As String
Dim FormatNum As String

ActiveDocument.Unprotect

Set TelRange = Selection.Paragraphs(1).Range
Set TelRange = TelRange.FormFields(1).Range

FirstNum = TelRange.Text
'Remove formatting if just changing already
'formatted number
FirstNum = Replace(FirstNum, "(", "")
FirstNum = Replace(FirstNum, ")", "")
FirstNum = Replace(FirstNum, "-", "")
FirstNum = Replace(FirstNum, " ", "")

'Must have digits
If Not IsNumeric(FirstNum) Then
MsgBox "You must input only digits in " _
& "this field.", vbExclamation, "Digits"
GoTo LeaveSub
End If

'Must have 10 digits
If Len(FirstNum) < 10 _
Or Len(FirstNum) > 10 Then
MsgBox "You must input exactly 10 digits in " _
& "this field.", vbExclamation, "Digits"
GoTo LeaveSub
End If

FormatNum = "(" + Left(FirstNum, 3) + ") " _
+ Mid(FirstNum, 4, 3) + "-" + _
Right(FirstNum, 4)
TelRange.FormFields(1).Result = FormatNum

LeaveSub:
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub
'_______________________________________

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


jgetch said:
I have forms that ask for telephone numbers, & would like to be able to
key in the 10 digits without having to add any formating, i.e. entering
8576571234 would appear as (857) 657-1234. This is a feature that works
well in Excel, but I do not see a similar feature in Word 2003.
 
If you're using a protected form, a macro is unnecessary, as you can define
the desired number format as part of the Form Field Options.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Hi Suzanne,

I checked, but I could not find the (xxx) xxx-xxxx format in the dropdown
list when using Number as a data type in the form field property dialog.
How do you use that format with a form field? Or is it new to 2003?

TIA

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
You aren't limited by the ones offered; you can create a custom one.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Ha! Forgot about that!

But what do you type in the format box to get any 10 digit number to come
out as
(###) ###-####
?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
Just that, I believe. I used ###-###-#### (those are hyphens between the
numbers) in one of my forms, and the results were as follows:

1234567890 becomes 123-456-7890

123/456-7890 becomes 123-456-7890

(123) 456-7890 becomes 123-456-7890

123.456.7890 becomes 123-456-7890

Etc. You get the idea. I limited the field to 14 characters, which will
allow (123) 456-7890, but it is still converted to the 12-character result.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Got it!

I do not know why, I kept typing the format as
(###) ###-###0

Of course, with the "0" at the end, Word treated it like a real number and
had trouble parsing the 4-digit group into a real number! I kept getting
stuff like 1-234-567-890 instead of (123) 456-7890.

As soon as I typed
(###) ###-####
it worked...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
I used 000-000-0000 as the display text in my form to encourage users to
include the area code--obviously, if they do not, the number won't turn out
right!

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Back
Top