Subscribe | Alerts via Email
View All Quotes
“We have to stop optimizing for programmers and start optimizing for users.”
-Jeff Atwood
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

©2010 Cal Zant
Sign In
Total Posts: 106
This Year: 5
This Month: 1
This Week: 0
Comments: 2

This is based on a post by Vik Thairani that I have referred back to many times, and I just updated it for Outlook 2007 (he actually found the VB script here).  A lot of times I am frustrated when I enter a phone number in my phone (AT&T 8525 running Windows Mobile Pocket PC 5.0), because it always defaults to display contact names by "Last, First."  It seems a lot more intuitive to me if everyone is listed "First Last."  Outlook gives you the option to configure what you want the default format to be, but that functionality isn't available on my phone.  So when the two sync up, I have some listed one way and others listed differently.  The steps below will install a VB Script in Outlook that you can run anytime and it will spin through your every contact in your default contact folder and change them to all display in the "First Last" format ... instead of editing each one.

1. Set your default preference in Outlook
Go to Tools > Options > Contact Options
Set the Default "File As" Order

2. Setup Security to Allow Unsigned Macros
Go to Tools > Macro > Security
Change to "Warnings for all macros"
Restart Outlook

3. Creating the Macro
Go to Tools > Macro > Visual Basic Editor
In the Left hand window double click on "ThisOutlookSession" (you may have to expand the project tree)
Copy and paste the following script into the code window:

Public Sub FormatNamesAndNumbers()
    Dim objOL As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim objContact As Outlook.ContactItem
    Dim objItems As Outlook.Items
    Dim objContactsFolder As Outlook.MAPIFolder
    Dim obj As Object
    On Error Resume Next
    Set objOL = CreateObject("Outlook.Application")
    Set objNS = objOL.GetNamespace("MAPI")
    Set objContactsFolder = objNS.GetDefaultFolder(olFolderContacts)
    Set objItems = objContactsFolder.Items
    For Each obj In objItems
        If obj.Class = olContact Then
            Set objContact = obj
            With objContact
                ' Try to file the contact by their first name followed by their last name.
                ' If one of those names are missing, just use the one that is there, but if
                ' both are missing file it by the company name set for the contact.
                If .FirstName <> "" Or .LastName <> "" Then
                    .FileAs = Trim(.FirstName & " " & .LastName)
                Else
                    .FileAs = .CompanyName
                End If
                
                ' Format all of the common types of phone numbers to be in the standard
                ' (XXX)XXX-XXXX format
                If .MobileTelephoneNumber <> "" Then _
                    .MobileTelephoneNumber = FormatPhoneNumber(.MobileTelephoneNumber)
                If .BusinessTelephoneNumber <> "" Then _
                    .BusinessTelephoneNumber = FormatPhoneNumber(.BusinessTelephoneNumber)
                If .HomeTelephoneNumber <> "" Then _
                    .HomeTelephoneNumber = FormatPhoneNumber(.HomeTelephoneNumber)
                 
                .Save
            End With
        End If
        Err.Clear
    Next
    Set objOL = Nothing
    Set objNS = Nothing
    Set obj = Nothing
    Set objContact = Nothing
    Set objItems = Nothing
    Set objContactsFolder = Nothing
End Sub

Private Function FormatPhoneNumber(ByVal number)

    Dim defaultAreaCode As String
    Dim returnValue As String
    defaultAreaCode = "806"

    number = CStr(number)
    number = Replace(number, "-", "")
    number = Replace(number, "(", "")
    number = Replace(number, ")", "")
    number = Replace(number, "+1", "")
    number = Replace(number, " ", "")
    Select Case Len(number)
        Case 7
            ' The number doesn't include an area code ... append the default area code
            returnValue = "(" & defaultAreaCode & ") " & _
                            Mid(number, 1, 3) & "-" & Mid(number, 4, 4)
        Case 10
            returnValue = "(" & Mid(number, 1, 3) & ") " & _
                            Mid(number, 4, 3) & "-" & Mid(number, 7, 4)
        Case 11
            ' The number is prefixed with an unnecessary "1" for long distance
            returnValue = "(" & Mid(number, 2, 3) & ") " & _
                            Mid(number, 5, 3) & "-" & Mid(number, 8, 4)
        Case Else
            returnValue = number
    End Select
    FormatPhoneNumber = returnValue
End Function

4. Saving the Code and Running the Macro
Click File > Save
Close the editor window
Go to Tools > Macro > Macros
Select "ThisOutlookSession.FormatNamesAndNumbers" and click Run

That's it ... after the script completes all of your contacts will be in the "First Last" format, and the changes will be reflected on your phone next time you sync.  It is a good idea to change your macro security settings back to "Warnings for signed macros, all other macros are disabled", which you can do by repeating step 2 and choosing the appropriate option.

I also updated it to format mobile, home, and business phone numbers to be in the common (XXX)XXX-XXXX format.  Notice that the FormatPhoneNumber function has a default area code set in the first few lines.  It will append that to numbers that only have 7 digits (i.e. no area code).

Monday, October 01, 2007 6:47:33 AM (Central Standard Time, UTC-06:00)  #