Eugene Astafiev

How To: Create a new Outlook Appointment item

There are three ways of creating a new appointment item in Outlook. Today we will examine each of them in depth.

The first and the simplest way is to create an appointment item using the CreateItem method of the Application class. It accepts the OlItemType enumeration, which informs the above method what item type should be created and returns a newly created Outlook item.

C#:

private void CreateAppointmentUsingCreateItem()
{
    Outlook.AppointmentItem appItem = OutlookApp.CreateItem(
         Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;
    if (appItem != null)
    {
        appItem.Save();
        appItem.Display(true);
        Marshal.ReleaseComObject(appItem);
    }
}

VB.NET:
Private Sub CreateContactUsingCreateItem()
    Dim appItem As Outlook.AppointmentItem = Nothing
    appItem = OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
    If Not IsNothing(appItem) Then
        appItem.Save()
        appItem.Display(True)
        Marshal.ReleaseComObject(appItem)
    End If
End Sub

The second way is to use the Items collections of the folder (Calendar folder in our case). The Add method accepts the OlItemType enumeration too and returns a newly created Outlook item.

C#:

private void CreateAppointmentUsingItemsAdd()
{
     Outlook.NameSpace ns = null;
     Outlook.MAPIFolder calendarFolder = null;
     Outlook.Items items = null;
     Outlook.AppointmentItem appItem = null;
     try
     {
         ns = OutlookApp.GetNamespace("MAPI");
         calendarFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
         items = calendarFolder.Items;
         appItem = items.Add(Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;
         appItem.Save();
         appItem.Display(true);
     }
     catch (Exception ex)
     {
         System.Windows.Forms.MessageBox.Show(ex.Message);
     }
     finally
     {
         if (appItem != null) Marshal.ReleaseComObject(appItem);
         if (items != null) Marshal.ReleaseComObject(items);
         if (calendarFolder != null) Marshal.ReleaseComObject(calendarFolder);
         if (ns != null) Marshal.ReleaseComObject(ns);
     }
}

VB.NET:
Private Sub CreateAppointmentUsingItemsAdd()
    Dim ns As Outlook.NameSpace = Nothing
    Dim calendarFolder As Outlook.MAPIFolder = Nothing
    Dim items As Outlook.Items = Nothing
    Dim appItem As Outlook.AppointmentItem = Nothing
    Try
        ns = OutlookApp.GetNamespace("MAPI")
        calendarFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
        items = calendarFolder.Items
        appItem = items.Add(Outlook.OlItemType.olAppointmentItem)
        appItem.Save()
        appItem.Display(True)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(appItem) Then Marshal.ReleaseComObject(appItem)
        If Not IsNothing(items) Then Marshal.ReleaseComObject(items)
        If Not IsNothing(calendarFolder) Then Marshal.ReleaseComObject(calendarFolder)
        If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns)
    End Try
End Sub

The third way is to use a template for a new Appointment item. The CreateItemFromTemplate method of the Application class does the job. You just need to pass a string (the filepath to a template). The template can be either in the .oft or .msg file formats (I have tested the method for both formats in my Outlook 2010 x64). It returns a newly created Outlook item based on the specified template.

C#:

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

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

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

9 Comments

  • ra says:

    I am trying to create and appointment using vb.net 2003 and trying to save it on provided receipients but its not working please can you help me.

  • Eugene Astafiev says:

    Hello Ra,

    Sorry, but I can’t diagnose the issue out of my head. Did you try to debug? Do you get any exceptions or error messages?

  • Ryan says:

    OutlookApp.CreateItem just threw an error on me for the first example in C#. I had to add Microsoft.etc.etc.Outlook.AppointmentItem to make all of the Outlooks work. What is the OutlookApp coming from?

  • Eugene Astafiev says:

    Hello Ryan,

    Did you try to debug? What method fires an exception?

    The OutlookApp property belongs to the add-in module class and used in Add-in Express based add-ins.

  • Jared says:

    Great code,

    I’ve been messing around and i want to create appointments- this works good for the individual but i want to create appointments for shared resources such as Rooms etc… any idea how i would go about that and how can i check resources availability?

    Like it would be cool to provide 2 datetimes and then the function returns an array of all the resources available in that time period.

    thanks

  • Dmitry Kostochko (Add-in Express Team) says:

    Hi Jared,

    The point is that Outlook does not keep track of resources such as rooms. The Location property in the Outlook Object Model is merely a string. That is why you will need to write your own function that will know about all your resources and return a value indicating whether a particular resource is available or not in StartTime <-> EndTime time interval.

  • ram says:

    i am able to create an appointment in my local system for my self. If i give someone else emails address to create appointment on their calendar then its throwing an error while adding the appointment to Shared calendar. Exception is “Operation Failed”

  • Andrei Smolin (Add-in Express Team) says:

    Hello Ram,

    I suggest that you talk to an admin to make sure that they have permissions.

  • Joe says:

    I am trying to

Post a comment

Have any questions? Ask us right now!