C# Programmatically Manage Contacts in Microsoft Outlook

You can manage contacts in Microsoft Outlook (2000 or higher) using C# and Outlook COM Interop. Microsoft has made it easy with Outlook 2003 and Outlook 2007. There is an interop class library already available in Visual Studio so you don't have the generate the Interop wrapper yourself. In order to set this up, the first step is to add a reference to the Outlook Interop DLL in your .NET C# project. Go to "Add References" and choose the following:

set reference to outlook interop



Notice that there are 2 different versions of the interop components. 12.0.0.0 is the component for Office 2007 so that is the one I chose. The component's name is "Microsoft.Office.Interop.Outlook".

Now you can add some code to start using this component. First, add a "using" reference at the top of your class to lessen the amount of typing and prevent any namespace conflicts. Here's the way to make a using reference alias:

// use alias for the outlook namespace
using OutlookInterop = Microsoft.Office.Interop.Outlook;

Now, we need to create an instance of the Outlook Application class to interact with it. Add this code to create a new instance and then get the path to the contacts folder in MAPI because that is what we want to interact with.

// create new instance of the Outlook app
OutlookInterop.ApplicationClass app = new OutlookInterop.ApplicationClass();
// get path to the MAPI api
OutlookInterop.NameSpace ns = app.GetNamespace("MAPI");
 
// get path to the contacts folder.  Use the OlDefaultFolders enum to specify
// that we want to get the olFolderContacts folder.
OutlookInterop.MAPIFolder contactsFolder = app.ActiveExplorer().Session.GetDefaultFolder(
    OutlookInterop.OlDefaultFolders.olFolderContacts);

Now we can start doing different tasks with our contacts directly in Outlook. Make sure to use the code above before these lines.

Search for a contact in Outlook

// Search for a contact by first and last name in outlook.
OutlookInterop.Items contactItems = contactsFolder.Items;
 
// set the first and last name to search for
string sFirstName = "Bob";
string sLastName = "Sanders";
// to search outlook, you must use the search format shown here where you specify the 
// field name to search in [] and the value to search in ''.
string sSearch = String.Format("[FirstName]='{0}' and " + "[LastName]='{1}'", sFirstName, sLastName);
 
// perform the search.  If found, contact will be filled with the contact record.  Returns 
// null if nothing is found.
OutlookInterop.ContactItem contact = (OutlookInterop.ContactItem)contactItems.Find(sSearch);
if (contact != null)
{
    // found a contact by that name so get the contact details.
    string sCompanyName = contact.CompanyName;
}

Get list of all contacts in Outlook

// Get list of all contacts in Outlook.
OutlookInterop.Items contactItems = contactsFolder.Items;
// loop the list of contacts returned
foreach (OutlookInterop.ContactItem foundContact in contactItems)
{
    // do something with each contact
    string sFirstName = foundContact.FirstName;
}

Create a new contact and add it to Outlook

// Create a new contact and add it to Outlook.
// create a new contact object to add.
OutlookInterop.ContactItem newContact = (OutlookInterop.ContactItem)app.CreateItem(
    OutlookInterop.OlItemType.olContactItem);
 
// fill in some properties you want to set for this object.
newContact.FirstName = "Bob";
newContact.LastName = "Sanders";
 
// save the contact to Outlook
newContact.Save();

Delete a contact from Outlook

// Delete a contact from Outlook.
// get list of all contacts in outlook
OutlookInterop.Items contactItems = contactsFolder.Items;
// loop all contacts
foreach (OutlookInterop.ContactItem foundContact in contactItems)
{
    // delete the contact if first name = "sanders"
    if (foundContact.LastName == "Sanders")
    {
        foundContact.Delete();
    }
}

NOTE: When you try to do dangerous things like deleting or adding contacts in outlook, the Microsoft Outlook security model prevents this from occurring until the user agrees. So you will see an annoying dialog like this:

outlook security warning dialogs

There really isn't anything you can do about this. At least, there is no way to change the Outlook settings to turn it off easily. The only real work arounds here are to either write some automation to send windows messages to the buttons to click them automatically or there is a free utility application you can download and run in your system tray to automatically click this box for you when it comes up. It's called "ClickYes" and is specifically designed for this purpose. You can download the utility here:

ClickYes Express

http://www.contextmagic.com/express-clickyes/

You have to pay for the Pro version which is the only one that supports Outlook 2007 but older versions of Outlook are free for this utility.