Customizing the Filing Format for Outlook 2007 Contacts

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Summary:  Learn how to quickly change the format in which Microsoft Office Outlook 2007 files and displays your contacts without fixing every individual contact. The solution requires only setting a global option for new contacts, and running a short macro to fix existing contacts.

Applies to:  Microsoft Office Outlook 2007
Published:  February 2009 | Updated:  June 2010
Provided by:  Angela Chu-Hatoun, Microsoft Corporation

Contents

  • Overview

  • Setting a Display Format for New Contacts

  • Changing the Display Format for Existing Contacts

  • Running the Macros

  • Conclusion

  • About the Author

  • Additional Resources

Overview

By default, when you create a contact in Microsoft Office Outlook 2007, Outlook displays the new contact starting with the last name, followed by a comma, the first name, and any middle names. For some people, recalling the last name to retrieve their contacts may not be convenient, and they may prefer to have Outlook file and display their contacts by using the first name or the company name. This article describes how to change the format that Outlook uses to display the name for new contacts, and how to write a simple macro to change the display format of existing contacts.

Setting a Display Format for New Contacts

Outlook supports displaying and filing contacts in five formats:

  • LastName, FirstName

  • FirstName LastName

  • CompanyName

  • LastName, FirstName (CompanyName)

  • CompanyName (LastName, FirstName)

For example, you could add Chris Ashton, who works at the company Alpine Ski House, to your Outlook contacts. By default, Outlook displays the contact as Ashton, Chris in the contact explorer and contact inspector. Through the user interface (UI) in the contact inspector for Chris Ashton, you can change the File as field to display the contact in one of the following formats:

  • Chris Ashton

  • Alpine Ski House

  • Ashton, Chris (Alpine Ski House)

  • Alpine Ski House (Ashton, Chris)

Alternatively, you can set an option in Outlook to display all new contacts in a specified format. For example, if you want Outlook 2007 to display the contact name as Chris Ashton instead of Ashton, Chris, follow these steps.

To change the name display format by using the UI

  1. On the Tools menu, click Options.

  2. In the Options dialog box, click Contact Options.

  3. In the Contact Options dialog box, select First Last in the Default "File As" order drop-down list.

When you specify the name for a contact, you type the name in the Full Name field in the contact inspector. By default, Outlook identifies the first word that you type as the first name, the last word as the last name, and any words in between, if they exist, as middle names. This input format is a separate option from the display format or filing format of the name. Although Outlook has a default input format, you can specify one of three ways for Outlook to interpret your input in the Full Name field:

  • FirstName MiddleNames LastName (This is the default input format.)

  • LastName FirstName

  • FirstName LastName1 LastName2

To set the input format in Outlook 2007, follow these steps.

To change the name input format by using the UI

  1. On the Tools menu, click Options.

  2. In the Options dialog box, click Contact Options.

  3. In the Contact Options dialog box, select a format in the Default "Full Name" order drop-down list.

Changing the Display Format for Existing Contacts

The previous section explained how to display Outlook contact names in a specific format for new contacts that you create. If you have to change the display format for existing contacts, you have two options: you can either change the File as field for individual contacts, which can be time-consuming, or run a macro that changes the display format for all existing contacts in a Contacts folder.

This section contains two code samples, written in Visual Basic for Applications (VBA).

The first code sample shows how to change the display format of each contact in the default Contacts folder to the first name followed by the last name.

The second code sample first checks whether both first and last names are empty and, if they are, the code checks whether there is a company name. In this case, the company name will be used as the display format.

There are several notes to point out:

  • The code samples call the GetDefaultFolder method of the NameSpace object to obtain the default Contacts folder for the user who is currently logged on to Outlook.

  • The code samples use the FileAs property of the ContactItem object to specify the string to file the contact by.

  • Because Outlook folders can contain heterogeneous items, the Contacts folder might contain other Outlook items, such as mail items, in addition to contact items. To ensure that the code modifies only contact items, it filters based on the message class IPM.Contact. For more information about Outlook message classes, see Item Types and Message Classes.

The following code sample considers only the first name and the last name entered for a contact. It does not process or display the middle name.

Private Sub ReFileContacts()
    Dim items As items, folder As folder
    Dim contactItems As Outlook.items
    Dim itemContact As Outlook.ContactItem

    Set folder = Session.GetDefaultFolder(olFolderContacts)
    Set items = folder.items
    Count = items.Count
    If Count = 0 Then
        MsgBox "Nothing to do!"
        Exit Sub
    End If

    ' Filter on the message class to obtain only contact items in the folder.
    Set contactItems = items.Restrict("[MessageClass]='IPM.Contact'")
  
    For Each itemContact In contactItems
       itemContact.FileAs = itemContact.FirstName + " " + itemContact.LastName
       itemContact.Save
    Next

    MsgBox "Your contacts have been refiled."
End Sub

The following code sample considers the company name entered for a contact, if there are no first and last names. It does not process or display the middle name.

Private Sub ReFileContacts()
    Dim items As Outlook.Items
    Dim folder As Outlook.Folder
    Dim contactItems As Outlook.Items
    Dim itemContact As Outlook.ContactItem

    Set folder = Application.Session.GetDefaultFolder(olFolderContacts)
    Set items = folder.items
    If items.count = 0 Then
        MsgBox "Nothing to do!", vbInformation
        Exit Sub
    End If

    ' Filter on the message class to obtain only contact items in the folder.
    Set contactItems = items.Restrict("[MessageClass]='IPM.Contact'")
  
    For Each itemContact In contactItems
        If itemContact.FirstName = "" And itemContact.LastName = "" Then
            If itemContact.CompanyName <> "" Then
                itemContact.FileAs = itemContact.CompanyName
            End If
        Else
            itemContact.FileAs = Trim(itemContact.FirstName + " " + itemContact.LastName)
        End If
        itemContact.Save
    Next

    MsgBox "Your contacts have been refiled.", vbInformation
End Sub

You have to run the code samples as Outlook macros only one time to change the way Outlook files and displays existing contacts.

Running the Macros

The default macro security setting is Warnings for signed macros; all unsigned macros are disabled. This setting allows a macro to run without any security prompts if the macro is digitally signed by a trusted publisher. All unsigned macros are disabled without notification. Therefore, unless your code sample is signed by a trusted publisher, you will be unable to run the code sample with this security setting. For more information about trusted publishers, see Add, remove, or view a trusted publisher.

The code samples in this article are not digitally signed. Therefore, you should set macro security to the Warnings for all macros option to run only these code samples. After changing to this option, restart Outlook. The Warnings for all macros option initially disables all macros. The first time that you attempt to run a macro or start the Visual Basic Editor, the Microsoft Office Outlook Security Notice dialog box appears. Click Enable Macros.

Important noteImportant

Because the Warnings for signed macros; all unsigned macros are disabled security setting provides better security for Outlook than the Warnings for all macros setting, set your macro security to Warnings for signed macros; all unsigned macros are disabled after you run these code samples. Restart Outlook for this setting to take effect.

To change the macro security setting

  1. On the Tools menu, click Trust Center.

  2. Click Macro Security.

  3. Select Warnings for all macros.

  4. Click OK.

  5. Restart Outlook.

To run the code samples as Outlook macros

  1. In Outlook 2007, click Alt+F11 to start the Visual Basic Editor.

  2. If this is the first time that you have started the Visual Basic Editor in the current Outlook session, the Microsoft Office Outlook Security Notice dialog box appears. Click Enable Macros.

  3. In the Project Explorer, expand the Project1 node.

  4. Expand the Microsoft Office Outlook Objects node.

  5. Double-click ThisOutlookSession.

  6. On the Tools menu, click References.

  7. In the References dialog box, ensure that Microsoft Outlook 12.0 Object Library and Visual Basic for Applications are selected.

  8. Click OK to close the References dialog box.

  9. To create a macro, copy and paste one of the previous code samples into the Code window.

  10. Press F5 to run the macro.

  11. Close the Visual Basic Editor.

To reset macro security after running this sample

  1. On the Tools menu, click Trust Center.

  2. Click Macro Settings.

  3. Select Warnings for signed macros; all unsigned macros are disabled.

  4. Click OK.

  5. Restart Outlook.

Conclusion

This article describes how to use Outlook user-interface options and two short macros that you can run in the Visual Basic Editor to change the format in which Outlook files and displays contacts.

About the Author

Angela Chu-Hatoun is a programmability writer for Outlook.

Additional Resources