Eugene Astafiev

How To: Create a new Outlook Contact item programmatically

Not so long ago I described various ways of creating Outlook messages programmatically. Today we will examine contact items. So, let’s submerge in coding!

The first and easiest way of creating a new Contact item in Outlook is to use the CreateItem method of the Application class. This method accepts the OlItemType enumeration and returns a newly created item. In one of my posts I described How to create and show a new Outlook message programmatically. Now let’s see how we handle Outlook contact items.

C#:

private void CreateContactUsingCreateItem()
{
    Outlook.ContactItem contact = OutlookApp.CreateItem(Outlook.OlItemType.olContactItem)
           as Outlook.ContactItem;
    if (contact != null)
    {
        contact.Save();
        contact.Display(true);
        Marshal.ReleaseComObject(contact);
    }
}

VB.NET:

Private Sub CreateContactUsingCreateItem()
    Dim contact As Outlook.ContactItem = Nothing
    contact = OutlookApp.CreateItem(Outlook.OlItemType.olContactItem)
    If Not IsNothing(contact) Then
        contact.Save()
        contact.Display(True)
        Marshal.ReleaseComObject(contact)
    End If
End Sub

Another way is to use the Add method of the Outlook Items class. Here I would like to draw your attention to the fact that a new contact item is always created in the Contacts folder regardless of the Items collection we use (the collection may not belong to the Contacts folder).

C#:

private void CreateContactUsingItemsAdd()
{
    Outlook.NameSpace ns = null;
    Outlook.MAPIFolder contactsFolder = null;
    Outlook.Items items = null;
    Outlook.ContactItem contact = null;
    try
    {
        ns = OutlookApp.GetNamespace("MAPI");
        contactsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
        items = contactsFolder.Items;
        contact = items.Add(Outlook.OlItemType.olContactItem) as Outlook.ContactItem;
        contact.Save();
        contact.Display(true);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (contact != null) Marshal.ReleaseComObject(contact);
        if (items != null) Marshal.ReleaseComObject(items);
        if (contactsFolder != null) Marshal.ReleaseComObject(contactsFolder);
        if (ns != null) Marshal.ReleaseComObject(ns);
    }
}

VB.NET:

Private Sub CreateContactUsingItemsAdd()
    Dim ns As Outlook.NameSpace = Nothing
    Dim contactsFolder As Outlook.MAPIFolder = Nothing
    Dim items As Outlook.Items = Nothing
    Dim contact As Outlook.ContactItem = Nothing
    Try
        ns = OutlookApp.GetNamespace("MAPI")
        contactsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
        items = contactsFolder.Items
        contact = items.Add(Outlook.OlItemType.olContactItem)
        contact.Save()
        contact.Display(True)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(contact) Then Marshal.ReleaseComObject(contact)
        If Not IsNothing(items) Then Marshal.ReleaseComObject(items)
        If Not IsNothing(contactsFolder) Then Marshal.ReleaseComObject(contactsFolder)
        If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns)
    End Try
End Sub

The third and the last one is to use a template item for creating a new contact item. The CreateItemFromTemplate method of the Application class does the job. In the list of my previous articles you can find the one which describes How to create a new Outlook message based on a template. Now I use a contact item instead of a regular message.

C#:

private void CreateContactUsingCreateItemFromTemplate()
{
    Outlook.ContactItem contact = null;
    try
    {
        contact = OutlookApp.CreateItemFromTemplate("D:\\Eugene Astafiev.oft")
             as Outlook.ContactItem;
        contact.Save();
        contact.Display(true);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (contact != null) Marshal.ReleaseComObject(contact);
    }
}

VB.NET:

Private Sub CreateContactUsingCreateItemFromTemplate()
    Dim contact As Outlook.ContactItem = Nothing
    Try
        contact = OutlookApp.CreateItemFromTemplate("D:\\Eugene Astafiev.oft")
        contact.Save()
        contact.Display(True)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(contact) Then Marshal.ReleaseComObject(contact)
    End Try
End Sub

As you can see, in the code above I used an oft file as a base for our contact item. My tests against Outlook 2010 show that you can pass an msg file as a template too.

See you on our forums and in the e-mail support!

6 Comments

  • IT Consultant says:

    Hello Eugene Astafiev,

    Hope you are doing well. Also thanks for the valuable code information you have shared.

    I need your help in saving the VCF file in outlook where i am getting the error.

    My code is as below

    Outlook._Application oApplication = new Outlook.ApplicationClass();
    Outlook._NameSpace oNamespace = oApplication.GetNamespace(“MAPI”);
    oNamespace.Logon(“Outlook”, “”, false, true);
    Outlook.MAPIFolder _olContacts = oNamespace.GetDefaultFolder(Outlook.
    OlDefaultFolders.olFolderContacts);
    Outlook.ContactItem contact = oApplication.Session.OpenSharedItem(@”C:\abc.vcf”)
    as Outlook.ContactItem;
    _olContacts.Items.Add(contact);
    contact.Save();

    In above code i am getting an error on line of code mentioned below.
    oApplication.Session.OpenSharedItem(@”C:\abc.vcf”) \

    Exception message comes as “Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

    Using C# 2.0 with added reference of
    Microsoft.Office.Interop.Outlook (version 12.0)
    Interop.Microsoft.Office.Core (version 2.4)
    OS = Windows 7 (64 Bit)
    Outlook 2003 Version

    Hope to hear your expert advise on same.

    Thanks & Take care.

  • Eugene Astafiev says:

    Hi,

    The problematic line of code consists of two pieces:

    1. oApplication.Session
    2. Session.OpenSharedItem(@”C:\abc.vcf”)

    Which part of the code fires the exception?

    Please try to use the existing object for calling the OpenSharedItem method:

    oNamespace.OpenSharedItem(@”C:\abc.vcf”)

    Does it help?

    Also please take a look at the “How to: Import Saved Items using OpenSharedItem” article at https://msdn.microsoft.com/en-us/library/bb176433.aspx. It provides the common error codes for the OpenSharedItem method.

    Finally, I have noticed that you use Windows 7. Did you try to move the .vcf file to another drive and then read it? Does it work?

  • ????????? says:

    ?????? ????? ?????? contact.Display
    ? ???????? ???????????? ???? ????????
    ??????????? COM ??????(???? ? ?????????? ?????)
    ? ??? ???? ?????????? ? ??? ????????.
    ???????.

  • Eugene Astafiev says:

    Hello,

    Thank you for your interest in our technical blog.

    Sorry, but I see a lot of question marks. Please try to post your question in English instead. Thank you.

  • Aleks says:

    In my program there is a component such as TOutlookApplication
    I do(make) so
    ns: NameSpace;
    ns: = fm1.OutlookApplication1.GetNamespace(WideString(‘MAPI’));
    That is I start server
    In the dispatcher of tasks I see start of process Outlook.exe
    …………………….
    Further I call a window ??????? for change

    fldr: = ns. GetDefaultFolder (Outlook.OlDefaultFolders.olFolderContacts);
    contitem: = fldr. Items. Item (ind) as ContactItem; // contact to the certain index
    contitem. Display (false);

    The window of contact Outlook opens
    I make changes and I close with preservation.

    Thus I see In the dispatcher of tasks that process Outlook.exe is not present,
    And the work with it server is necessary to me.
    How to be and what it is wrong?
    Thank.

  • Eugene Astafiev says:

    Hello Aleks,

    What Outlook version do you have installed on the PC? Is it Outlook 2010?

    Anyway, please remember that I am a .Net developer (not Delphi). That is why I would recommend contacting our support service department for investigating the issue in depth. Please use our forums or support e-mail address.

Post a comment

Have any questions? Ask us right now!