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!

8 Comments

  • https://secure.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=32 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.

  • https://secure.gravatar.com/avatar/bbd979ae5b772d0bc1f82065aa4ba146?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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?

  • https://secure.gravatar.com/avatar/4d3ee821983fbf8a75ea9268ee499c02?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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?

  • https://secure.gravatar.com/avatar/bbd979ae5b772d0bc1f82065aa4ba146?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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.

  • https://secure.gravatar.com/avatar/8c0730dff814aff4c38580057d6c78a4?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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.

  • https://secure.gravatar.com/avatar/8010536b9429c4575ab13e3569bb5256?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G 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”

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Ram,

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

Post a comment

Have any questions? Ask us right now!