Below are some examples for using the Contact List in Outlook.  Before you can use these example you must set a reference to the Outlook Object Library.

Create a Contact

Sub MakeContact()

    Dim olApp As Outlook.Application
    Dim olCi As Outlook.ContactItem

    Set olApp = New Outlook.Application
    Set olCi = olApp.CreateItem(olContactItem)

    With olCi
        .FirstName = "Dick"
        .LastName = "Kusleika"
        .MobileTelephoneNumber = "(402) 555-1212"
        .Email1Address = ""
        .HomeAddressStreet = "111 S 1st"
        .HomeAddressCity = "Omaha"
        .HomeAddressState = "NE"
        .HomeAddressPostalCode = "68100"
        .SelectedMailingAddress = olHome
        .Categories = "Business, Personal"
    End With

    Set olCi = Nothing
    Set olApp = Nothing

End Sub


The CreateItem method is used to create the new contact.  This method can be used to create just about anything in Outlook; Emails, Tasks, Journal Entries, etc.  olContactItem is a built in constant that tells VBA which type of item to create.

The With/End With block shows some typical properties that can be set for a ContactItem object.  Note that to add multiple Categories, which takes a String data type, pass a comma delimited list.

The .SelectedMailingAddress property is set using a built-in constant, olHome.  The other options are olBusiness, olOther, and olNone.  Note that if you use late binding, you must convert any built-in constants that you use to their numeric equivalent.

Don't forget to .Save, or at least .Display, before your procedure completes.

Prev Page: Calendar        Top of This Page        Next Page: Tasks


Retrieving a Contact

Sub GetContact()

    Dim olApp As Outlook.Application
    Dim olNs As NameSpace
    Dim Fldr As MAPIFolder
    Dim olCi As ContactItem

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.Folders("Personal Folders").Folders("Contacts")

    For Each olCi In Fldr.Items
        If olCi.NickName = "DoubleD" Then
            Debug.Print olCi.FullName, olCi.Email1Address
        End If
    Next olCi

    Set olCi = Nothing
    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing

End Sub


The NameSpace object isn't a typical object.  There is no physical representation of the NameSpace as there is for a Folder or an AppointmentItem.  It serves as a gateway for access to much of the objects in Outlook's object model.  To get at the NameSpace, you use the GetNameSpace method of the Application object.  The only argument that is supported for this method is "MAPI", so that makes it nice and easy.

The Folder object represents actual folders in Outlook.  To see these folders, choose Folder List from the View menu in Outlook.  Outlook provides a method of the NameSpace object called GetDefaultFolder.  The GetDefaultFolder method is useful when you need a reference to a default folder quickly.  Sometimes you need to get a folder that isn't a default folder, and that's when nesting Folders properties as in the example above does the trick.  You can, however, start with a default folder to reference folders beneath it in the folder hierarchy, as in this example:

Set Fldr = olNs.GetDefaultFolder(olFolderContacts).Folders("SecretStuff")

The meat of the procedure simply uses a For Each statement to loop through all of the ContactItems in the Folder.  When it finds one that satisfies the criteria, it prints certain properties to the Immediate Window.  This example tests for a NickName of "DoubleD".  You can test for whatever properties that suit your situation.  Also, once the object variable, olCi, points to the correct ContactItem, you can do anything you want to it.  You can write its properties to cells or even change its properties.  If you change anything, be sure to use the .Save method or you will be disappointed.

Prev Page: Calendar        Top of This Page        Next Page: Tasks




                    Contact Me            Visit Daily Dose of Excel - an Excel weblog