March 9, 2021
Hot Topics:

Interacting with Outlook from a WinForms Application

  • By Rachel Wireman
  • Send Email »
  • More Articles »

Creating a Simple Outlook Contact Management Application

To demonstrate some of the functionality of the Outlook interop class, you will create a "contact management" application that enables a user to choose an Outlook folder from which to view contacts, see the information for each contact when hovering over the name, and to alter some of the contacts' properties. The sample will introduce you to the Outlook classes and how easy it can be to come up with your own uses for interacting with Outlook (and, by extension, Office) from your .NET applications.

The application, on first glance, will have an empty list box and a button to "Get Contacts" like so:

Click here for a larger image.

When the user clicks the button, he will be presented with an Outlook-style folder picker to choose the folder from which he wants to "import" contacts. The Outlook interop class exposes a PickFolder method of its Namespace class to provide this functionality with one line of code. First, however, you need to create an Outlook Application object that will expose the Outlook functionality and get its Namespace object.

Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS    = oApp.GetNamespace("MAPI");

"MAPI" is the supported parameter for the GetNamespace method with Outlook. The following code and screenshot illustrate how you accomplish the task of allowing the user to choose an Outlook folder.

private Outlook.MAPIFolder oContactsFolder = oNS.PickFolder();

Click here for a larger image.

You will limit the "import" process to contact types, so if the user chooses a folder with no contacts in it, he will receive no results. The PickFolder method returns an object of type MAPIFolder. Once you have a reference to the desired folder, you can iterate through its items. Before doing so, you will restrict the items to the contact type.

string filter = "[MessageClass] = \"IPM.Contact\""
Outlook.Items oContactItems = oContacts.Items.Restrict(filter);

foreach (Outlook.ContactItem oContact in oContactItems)

The Restrict method takes a filter argument, a formatted string referencing the item's properties to search against in brackets with the search criteria in quotations. For more information on creating the filter string and for some examples, refer to the MSDN article.

The ContactItem class exposes properties of the contact such as:

  • LastName, FirstName, MiddleName, Title
  • JobTitle
  • Email1Address, Email1DisplayName, IMAddress
  • WebPage
  • BusinessTelephoneNumber, OtherTelephoneNumber, PagerNumber, MobileTelephoneNumber, BusinessFaxNumber

The list goes on, but the above enumerates the properties you will use for the example. The Email1Address, Email1DisplayName, and IMAddress properties are special in this list. They are properties that, when accessed, will cause Outlook to prompt the user with the security warning mentioned above. The others will not.

Page 2 of 4

This article was originally published on September 24, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date