Create a recurring appointment by using the default recurrence pattern

This example shows how to create a recurring appointment by using the default recurrence pattern.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

When you create an appointment in Outlook, you are creating an AppointmentItem object. Your appointment is a recurring appointment if the IsRecurring property of the AppointmentItem is set to true. IsRecurring cannot be set directly.

However, you can create a recurring appointment by using the RecurrencePattern object. To create a recurring appointment programmatically, create an AppointmentItem object, call the GetRecurrencePattern() method of the AppointmentItem object, and then save the AppointmentItem object. This creates an appointment that uses the default recurrence pattern, which occurs weekly on the day of the week for which the appointment was created, and has no end date. The RecurrencePattern object allows you to create recurring appointments at specified intervals—daily, weekly, monthly, or yearly. If you do not specify intervals for the RecurrencePattern object, Outlook will use the default recurrence pattern.

When you work with recurring appointment items, you should release any prior references, obtain new references to the recurring appointment item before you access or modify the item, and release these references as soon as you are finished and have saved the changes. This practice applies to the recurring AppointmentItem object, and any Exception or RecurrencePattern object. To release a reference in Visual Basic, set that existing object to Nothing. In C#, explicitly release the memory for that object.

Note that even after you release your reference and attempt to obtain a new reference, if there is still an active reference (held by another add-in or Outlook) to one of the above objects, your new reference will still point to an out-of-date copy of the object. Therefore, it is important that you release your references as soon as you are finished with the recurring appointment.

In the following example, CreateRecurringAppointment creates an AppointmentItem object. It then calls GetRecurrencePattern. GetRecurrencePattern returns a RecurrencePattern object, and the AppointmentItem is saved. This creates a recurring appointment that uses the default recurrence pattern.

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 CreateRecurringAppointment()
{
    Outlook.AppointmentItem appt = Application.CreateItem(
        Outlook.OlItemType.olAppointmentItem)
        as Outlook.AppointmentItem;
    appt.Subject = "Weekly Extensibility Team Meeting";
    Outlook.RecurrencePattern pattern = appt.GetRecurrencePattern();
    appt.Save();
}

See also