This documentation is archived and is not being maintained.

How to: Search for a Specific Contact

Note Required applications

The code example in this topic can be compiled only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005

    -or-

    Visual Studio Team System

  • Microsoft Office Outlook 2003

NoteNote

This code does not compile if you use the VSTO 2005 SE version of the Outlook 2003 add-in project template. For more information, see Getting Started Programming Application-Level Add-ins.

This example searches a Microsoft Office Outlook 2003 contacts folder for a specific contact by first and last name. The example assumes that a contact named John Evans exists in the contacts folder.

Example

private void ThisApplication_Startup(object sender, System.EventArgs e)
{
    FindContactEmailByName("John", "Evans");
}

private void FindContactEmailByName(string firstName, string lastName)
{
    Outlook.NameSpace outlookNameSpace = this.GetNamespace("MAPI");
    Outlook.MAPIFolder contactsFolder =
        outlookNameSpace.GetDefaultFolder(
        Microsoft.Office.Interop.Outlook.
        OlDefaultFolders.olFolderContacts);

    Outlook.Items contactItems = contactsFolder.Items;

    try
    {
        Outlook.ContactItem contact =
            (Outlook.ContactItem)contactItems.
            Find(String.Format("[FirstName]='{0}' and "
            + "[LastName]='{1}'", firstName, lastName));
        if (contact != null)
        {
            contact.Display(true);
        }
        else
        {
            MessageBox.Show("The contact information was not found.");
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}


See Also

Show: