Export (0) Print
Expand All

How to: Programmatically Access Outlook Contacts

This example finds all contacts whose last names contain a specified search string.

Applies to: The information in this topic applies to application-level projects for Outlook 2013 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    AccessContacts("Na");
}

private void AccessContacts(string findLastName)
{
    Outlook.MAPIFolder folderContacts = this.Application.ActiveExplorer().Session.
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
    Outlook.Items searchFolder = folderContacts.Items;
    int counter = 0;
    foreach (Outlook.ContactItem foundContact in searchFolder)
    {
        if (foundContact.LastName.Contains(findLastName))
        {
            foundContact.Display(false);
            counter = counter + 1;
        }
    }
    MessageBox.Show("You have " + counter +
        " contacts with last names that contain "
        + findLastName + ".");
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    AccessContacts("Na");
}

private void AccessContacts(string findLastName)
{
    Outlook.MAPIFolder folderContacts = this.Application.ActiveExplorer().Session.
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
    Outlook.Items searchFolder = folderContacts.Items;
    int counter = 0;
    foreach (Outlook.ContactItem foundContact in searchFolder)
    {
        if (foundContact.LastName.Contains(findLastName))
        {
            foundContact.Display(false);
            counter = counter + 1;
        }
    }
    MessageBox.Show("You have " + counter +
        " contacts with last names that contain "
        + findLastName + ".");
}

This example requires:

  • Contacts whose last names contain the string "Na" (for example, Tzipi Butnaru) in the Contacts folder.

Show:
© 2014 Microsoft