How to: Create a Custom Contact Item

This example shows how to create a custom contact item and set both predefined and user-defined properties.

Programming Applications for Office Outlook 2007

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007, from Microsoft Press (ISBN 9780735622494, copyright Microsoft Press 2007, all rights reserved).

Buy this book

Sample chapters

A ContactItem object represents a contact in the Contacts folder, and has more than 100 built-in properties such as FirstName and LastName. Sometimes, the built-in properties are not sufficient and you need to add custom properties, which you can do by using the UserProperties collection.

In the following code example, CreateCustomItem creates a custom ContactItem object, names it "Shoe Store", and calls the Add(String, Object) method to add it to a folder named "Shoe Store". CreateCustomItem first gets the "Shoe Store" folder by using the GetDefaultFolder(OlDefaultFolders) method. The "Shoe Store" folder is a subfolder of the default Contacts folder. CreateCustomItem then sets the FirstName and LastName properties, and creates a user-defined property ("Shoe Size") by using the UserProperties collection.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void CreateCustomItem()
{
    Outlook.Folder folder =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderContacts).Folders[
        "Shoe Store"] as Outlook.Folder;
    Outlook.ContactItem contact =
        folder.Items.Add(
        "IPM.Contact.Shoe Store") as Outlook.ContactItem;
    contact.FirstName = "Michael";
    contact.LastName = "Affronti";
    contact.UserProperties["Shoe Size"].Value = "9";
    contact.Save();
}

Other resources

Show:
© 2014 Microsoft