Working with Recurring Appointments

This section covers recurring appointments, including how to create a recurring appointment and how to deal with exceptions to the recurrence pattern.

Creating a Recurring Appointment

A recurring appointment is represented by an Appointmentltem object with the IsRecurring property set to True. However, you cannot set this property directly. Instead, you create a recurring appointment by calling the GetRecurrencePattern method of the Appointmentltem object and then saving the item. The following example illustrates how to create an appointment named Test Appointment and then call the GetRecurrencePattern method to make it a recurring appointment:

Sub CreateRecurringAppointment_Click Set Myltem = Application.Createltem(l) Myltem.Subject = "Test Appointment" Set MyPattern = Myltem.GetRecurrencePattern Myltem.Save End Sub

The GetRecurrencePattern method returns a RecurrencePattern object. You can change the recurrence pattern of the appointment by setting properties of the appointment's RecurrencePattern object.

Setting the Recurrence Pattern of an Appointment

When a new recurring appointment is created, it inherits a default recurrence pattern based on the time the appointment was created. To change the recurrence pattern of an appointment, set the appropriate properties of the appointment's RecurrencePattern object.

For more information about the properties of the RecurrencePattern object affecting the recurrence pattern, see Microsoft Outlook Visual Basic Reference Help.

The following example shows how to create a one-hour recurring appointment that occurs at noon on the second Tuesday of each month for two months, starting July 2001:

Sub SetRecurrencePattern_Click Set Myltem = Application.Createltem(l) Myltem.Subject = "Monthly Appointment" Set MyPattern = Myltem.GetRecurrencePattern MyPattern.RecurrenceType = 3 'Monthly, on a specific weekday MyPattern.Interval = 1 'Every month (2 would be every other month) MyPattern.DayOfWeekMask = 4 'Which day of week (Tuesday) MyPattern.Instance = 2 'Which instance (second Tuesday) MyPattern.StartTime = "12:00 pm" 'Time each appointment begins MyPattern.EndTime = "1:00 pm" 'Time each appointment ends MyPattern.PatternStartDate = #7/1/2001# 'Earliest date appt can occur MyPattern. Pattern End Date = #9/1/2001# 'Latest date appt can occur Myltem.Save End Sub

Working with a Single Appointment in a Series

To determine whether an instance of a recurring appointment occurs at a particular time, use the GetOccurrence method of the RecurrencePattern object. This method returns an Appointmentltem object representing the instance of the recurring appointment.

The GetOccurrence method will produce an error if an instance of the recurring appointment does not start at the date and time you provide. If it is possible that your script can supply a date and time that does not match an instance of a recurring appointment (because of user input, for example), the script should be able to handle the error appropriately. Otherwise, the script procedure will fail and Outlook will display an error message.

The following example illustrates how to use the GetOccurrence method to determine whether a recurring appointment (created by the code in the previous section) starts on a date and time provided by the user. The On Error Resume Next statement ensures that the procedure will continue if the user enters anything that does not match the start date and time of an instance of the recurring appointment. After calling the GetOccurrence method, the script tests the MyAppointment variable to determine whether it is set to Nothing, indicating that the method failed and did not return an Appointmentltem object.

Sub CheckOccurrence_Click Set MyNameSpace = Application.GetNameSpace("MAPI") Set MyCalendarFolder= MyNameSpace.GetDefaultFolder(9) Set Myltem = MyCalendarFolder.ltems("Monthly Appointment")

Set MyPattern = Myltem.GetRecurrencePattern On Error Resume Next

MyDate = InputBox ("Enter a date and time (m/d/yy hh:mm): ") Do While MyDate <> "" Set MyAppointment = Nothing

Set MyAppointment = MyPattern.GetOccurrence(MyDate) If TypeName(MyAppointment) <> "Nothing" Then MsgBox "This instance of " & MyAppointment.Subject _ & " occurs on " & MyAppointment.Start

Else

MsgBox Myltem.Subject & " does not occur on " & MyDate End If

MyDate = InputBox _ ("Enter another date and time (m/d/yy hh:mm):")

Loop End Sub

Once you retrieve the Appointmentltem object representing an instance of a recurring appointment, you can delete or change the appointment instance. When this happens, Outlook creates an Exception object. The properties of this object describe the changes that were made to the instance. All of the Exception objects for a recurring appointment are contained in an Exceptions collection object associated with the appointment's RecurrencePattern object.

The Appointmentltem property of the Exception object returns the Appointmentltem object that constitutes the exception to the original recurrence pattern of the recurring appointment. You can use the methods and properties of the Appointmentltem object to work with the appointment exception. The following example changes the subject of an instance of the recurring appointment created by the script in the previous section. It then uses the Appointmentltem property of the resulting Exception object to change the start time of the appointment exception.

Sub CreateException_Click Set MyNameSpace = Application.GetNameSpace("MAPI") Set MyCalendarFolder= MyNameSpace.GetDefaultFolder(9) Set Myltem = MyCalendarFolder.ltems("Monthly Appointment") Set MyPattern = Myltem.GetRecurrencePattern Set Mylnstance = MyPattern.GetOccurrence(#8/14/2001 12:00 pm#) Mylnstance.Subject = "Monthly Pattern (exception)" MsgBox Mylnstance.Subject & " starts on " & Mylnstance.Start Mylnstance.Save

Set MyNewPattern = Myltem.GetRecurrencePattern Set MyException = MyNewPattern.Exceptions.Item(1) Set MyNewlnstance = MyException.Appointmentltem MyNewlnstance.Start = (#8/14/2001 1:00 pm#)

MsgBox MyNewlnstance.Subject & " now starts on " & MyNewlnstance.Start MyNewlnstance.Save End Sub

The following two sections describe how to use the Exception object to work with changed or deleted instances of a recurring

This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks appointment.

Determining the Original Date of an Exception

The OriginalDate property of the Exception object returns the start date and time of the changed appointment before it was changed. The following example uses the OriginalDate property to retrieve the original start date of the appointment exception created in the previous section. In addition, it uses the Start property of the Appointmentltem associated with the Exception object to provide the new start date of the appointment.

Sub ShowOriginalDate_Click On Error Resume Next

Set MyNameSpace = Application.GetNameSpace("MAPI") Set MyCalendarFolder = MyNameSpace.GetDefaultFolder(9) Set Myltem = MyCalendarFolder.ltems("Monthly Appointment") Set MyPattern = Myltem.GetRecurrencePattern Set MyException = MyPattern.Exceptions.Item(1) MsgBox "The changed appointment originally occurred on " _ & MyException.OriginalDate & Chr(13) & "It now occurs on "_ & MyException.Appointmentltem.Start, vblnformation End Sub

Determining Whether an Appointment Instance Was Deleted

When an appointment in a recurring series is deleted, an Exception object representing the deleted appointment is created, and the Deleted property of the Exception object is set to True. The following example uses the Delete method of the Appointmentltem object to delete the appointment instance changed in the previous section. It then tests the value of the Deleted property of the Exception object representing the deleted appointment to determine whether the appointment was actually deleted.

Sub ChecklfDeleted_Click Set MyNameSpace = Application.GetNameSpace("MAPI") Set MyCalendarFolder = MyNameSpace.GetDefaultFolder(9) Set Myltem = MyCalendarFolder.ltems("Monthly Appointment") Set MyPattern = Myltem.GetRecurrencePattern Set MyAppointment = MyPattern.GetOccurrence(#8/14/2001 1:00 pm#) MyAppointment. Delete

Set MyException = MyPattern.Exceptions.Item(1) If MyException.Deleted Then MsgBox "The appointment was deleted." End Sub

0 0

Responses

  • may
    How to edit a recurring appointment using vba?
    8 years ago
  • Fastred
    How to set outlook appointment recurrence VBA?
    8 years ago
  • christine
    How to delete recurring meetings in outlook "VBA"?
    8 years ago
  • Benjamin
    What would a user do to make a onetime change to a series of recurring appointments in Outlook?
    5 years ago
  • albina
    How to set a recurring appointment in outlook vba?
    1 month ago

Post a comment