Eugene Astafiev

How To: Create a new recurring Outlook Appointment item

In my previous article I described how to create a new Outlook Appointment item. Now I want to show you the way to create a new recurring appointment item in Outlook. Unfortunately the Outlook Object Model doesn’t provide any intuitive property or method for such a task (for example, they could name it as Recurrent, SetRecurrent etc.). The only meaningful property provided by the ApppointmentItem class is IsRecurring. It returns a Boolean value indicating whether an appointment item is a recurrent appointment or not. So far, so good ;-) But how can we set the appointment to be recurring?

Then I started to dig deeper and learnt more about each method and property of the AppointmentItem class described in the Object Browser. I stumbled upon the GetRecurrencePattern method. It doesn’t accept any parameters and returns an instance of the RecurrencePattern class. If there is no recurrence pattern for the appointment item yet, a new instance is created. The object itself represents the recurrence attributes of a particular appointment item. So, I tried to run it and that was exactly what I’d been looking for! The IsRecurring property returns true after the GetRecurrencePattern method has been called.

The RecurrencePattern class provides a lot of important properties that can be set in case of a recurring appointment. Below is the list of properties I used in the sample code:

  • The RecurrenceType property allows you to specify the frequency of occurrences for the recurrence pattern. The OlRecurrenceType enumeration is used for this task (in the code I chose a weekly occurrence).
  • The StartTime and EndTime properties allow specifying the start and end time for the recurrent appointment respectively.
  • The Duration property allows you to set the duration (in minutes) for a recurring appointment item (for example, imagine a situation when the EndTime property was not specified).
  • The PatternStartDate and PatternEndDate properties are used for setting and getting the start and end date for the recurrence pattern.

Also the RecurrencePattern class provides one method named GetOccurrence. It allows you to get a specific instance of the AppointmentItem class on the specified date.

The following code creates a new recurring appointment item with the Subject property set to the “Meeting with the Boss” and then displays it to a user. The code is identical for Add-in Express based and VSTO based add-ins (I have tested it against both). You just need to pass an instance of the Outlook Application class to the CreateNewRecurringAppointment method to get it working!

C#:

private void CreateNewRecurringAppointment(Outlook._Application OutlookApp)
{
    Outlook.AppointmentItem appItem = null;
    Outlook.RecurrencePattern pattern = null;
    try
    {
        appItem = OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
           as Outlook.AppointmentItem;
        // create a recurrence
        pattern = appItem.GetRecurrencePattern();
        pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursWeekly;
        pattern.StartTime = DateTime.Parse("9:00:00 AM");
        pattern.EndTime = DateTime.Parse("10:00:00 AM");
        // we can specify the duration instead of using the EndTime property
        // pattern.Duration = 60;
        pattern.PatternStartDate = DateTime.Parse("11/11/2011");
        pattern.PatternEndDate = DateTime.Parse("12/25/2011");
        appItem.Subject = "Meeting with the Boss";
        appItem.Save();
        appItem.Display(true);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (pattern != null)
           System.Runtime.InteropServices.Marshal.ReleaseComObject(pattern);
        if (appItem != null)
           System.Runtime.InteropServices.Marshal.ReleaseComObject(appItem);
    }
}

VB.NET:

Private Sub CreateNewRecurringAppointment(OutlookApp As Outlook._Application)
    Dim appItem As Outlook.AppointmentItem = Nothing
    Dim pattern As Outlook.RecurrencePattern = Nothing
    Try
        appItem = OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
        ' create a reccurence
        pattern = appItem.GetRecurrencePattern()
        pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursWeekly
        pattern.StartTime = DateTime.Parse("9:00:00 AM")
        pattern.EndTime = DateTime.Parse("10:00:00 AM")
        ' we can specify the duration instead of using the EndTime property
        ' pattern.Duration = 60
        pattern.PatternStartDate = DateTime.Parse("11/11/2011")
        pattern.PatternEndDate = DateTime.Parse("12/25/2011")
        appItem.Subject = "Meeting with the Boss"
        appItem.Save()
        appItem.Display(True)
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(pattern) Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pattern)
        End If
        If Not IsNothing(appItem) Then
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appItem)
        End If
    End Try
End Sub

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

2 Comments

  • https://secure.gravatar.com/avatar/6054a05b7a17ff6a1b8c3e3b3a9409fa?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Turki Umairan says:

    In your post you said: You just need to pass an instance of the Outlook Application class to the CreateNewRecurringAppointment method to get it working!

    An example would have been very helpful.

    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 Turki,

    Please see the code sample below.

    C#
    Outlook.Application olApp = new Outlook.Application();
    CreateNewRecurringAppointment(olApp);
    Marshal.ReleaseComObject(olApp);

    VB.NET
    Dim olApp As Outlook.Application = New Outlook.Application
    CreateNewRecurringAppointment(olApp)
    Marshal.ReleaseComObject(olApp)

Post a comment

Have any questions? Ask us right now!