
Change Phone Number Format
Hi Chris.
I have a similar problem but it is that Fusion 1 sync software hands me
totally unformatted lits of digits as phone numbers.
The code here works fine for the conversion. To get it to do what you want
it to do just modofy the "Parsephone" function to test for the types of
numbers you want to convert and do the conversion. All of this utilizes
fairly strait-forward string handling techniques.
Good luck,
Rick
Public Sub FormatPhone()
'Reformat phone numbers in (nnn) nnn-nnnn format. This is useful to clean up
phone numbers
'imported from Sprint using Fusion One sync software
Dim myOlApp As Outlook.Application, myNameSpace As NameSpace, myFolder
As MAPIFolder
Dim myItem As ContactItem, myItems As Items
'Initialize Control Blocks
Set myOlApp = New Outlook.Application
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
Set myItems = myFolder.Items
'Cycle thru all contacts in default contact folder
For Each myItem In myItems
'Make sure this is a contact item
If myItem.Class = olContact Then
' Update phone number fields of interest
myItem.HomeTelephoneNumber =
ParsePhone(myItem.HomeTelephoneNumber)
myItem.BusinessTelephoneNumber =
ParsePhone(myItem.BusinessTelephoneNumber)
myItem.MobileTelephoneNumber =
ParsePhone(myItem.MobileTelephoneNumber)
myItem.OtherTelephoneNumber =
ParsePhone(myItem.OtherTelephoneNumber)
myItem.Close (olSave)
End If
Next myItem
Set myItem = Nothing
Set myItems = Nothing
Set myFolder = Nothing
Set myNameSpace = Nothing
Set myOlApp = Nothing
End Sub
Private Function ParsePhone(ByVal Phone As String) As String
' Convert phone number from nnnnnnnnnn to (nnn) nnn-nnnn
Dim i As Integer
' Set return for early exit
ParsePhone = Phone
' Make sure length is exactly 10 digits and it consists only of the
digits 0-9
If Len(Trim(Phone)) <> 10 Then Exit Function
For i = 1 To 10
If (Mid(Phone, i, 1) < "0") Or (Mid(Phone, i, 1) > "9") Then Exit
Function
Next i
' Reformat number and return
ParsePhone = "(" & Left(Phone, 3) & ")" & " " & Mid(Phone, 4, 3) & "-" &
Right(Phone, 4)
End Function
Quote:
> Hi!
> Call me unusual, but I've a penchant to change all my
> Contact phone numbers' formats from "(nnn) nnn-
> nnnn", "nnn/nnn-nnnn" and others to "nnn.nnn.nnnn". I
> think using a decimal provides the best readability.
> Anyway, I can't find any code to do this, and I am too
> much of a newbie to write it from scratch myself. Anyone
> got some code to share?
> Chris