Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)
The AppointmentItem object can be a single appointment, a recurring appointment, a single meeting, or a recurring meeting. Since Outlook supports recurring appointments, Outlook provides objects that allow you to manipulate the patterns for the recurrence of an appointment and objects to handle any exceptions to the recurrence. The following section covers some of the properties and methods that allow you to create and manipulate recurring appointments, create and manipulate meetings, and use the new real-time collaboration features of meetings in your applications.
Creating Basic and Recurring Appointments
Outlook provides an easy interface to create new appointments—even recurring appointments. The following section shows you how to create basic appointments and recurring appointments, and how to work with the recurrence pattern and exception objects of your recurring appointments. To begin, the following code sample shows you how to create a basic appointment in an Outlook calendar:
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oCalendar = oNS.GetDefaultFolder(9) set oItems = oCalendar.Items set oNewCal = oItems.Add oNewCal.Start = Date + 1 oNewCal.End = DateAdd("h",1,oNewCal.Start) oNewCal.Subject = "Training on Outlook" oNewCal.Display End Sub |
Outlook identifies recurring appointments when the IsRecurring property is True. You cannot set this property directly. Instead, to turn your current appointment into a recurring appointment, you call the GetRecurrencePattern method and set the properties of the returned RecurrencePattern object. The way you should set the properties on the RecurrencePattern object is to first set the RecurrenceType property. This property can be one of the following values: olRecursDaily (0), olRecursWeekly (1), olRecursMontly (2), olRecursMonthNth (3), olRecursYearly (5), or olRecursYearNth (6). You can also set the Interval property to tell Outlook the interval between occurrences. For example, if you set the RecurrenceType to 1 (olRecursWeekly), and then set the Interval property to be 3, Outlook will make the appointment recur every third week. You can also set other properties on the RecurrencePattern object, such as DayOfWeekMask or DayOfMonth. These masks tell Outlook on which day of the week or month the recurring appointment should occur. The easiest way to learn using RecurrencePatterns is to view a code sample and then start trying the different properties on your own. The following code sample shows you how to create a recurring appointment that starts on October 1, 1998, occurs weekly on Thursdays, starts at 3:00 PM and ends at 4:30 PM, and ends in the middle of December.
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oCalendar = oNS.GetDefaultFolder(9) set oItems = oCalendar.Items set oNewCal = oItems.Add oNewCal.Start = #10/01/98 3:00 PM# oNewCal.End = #10/01/98 4:30 PM# oNewCal.Subject = "Status Meetings" set oRecur = oNewCal.GetRecurrencePattern oRecur.RecurrenceType = 1 'Recur Weekly oRecur.DayOfWeekMask = 16 'Thursdays oRecur.PatternStartDate = #10/01/98# oRecur.Interval = 1 'Every Thursday oRecur.PatternEndDate = #12/18/98# oNewCal.Save oNewCal.Display End Sub |
Working with Recurring Appointments
There may be times when you need to work with existing recurring appointments in your application. To find out if an appointment is recurring, check the IsRecurring property to see if it is set to True. If it is, you can get a specific occurrence of the recurring appointment by using the GetOccurrence method. Note that the GetOccurrence method will return an error if the appointment does not occur on the date you pass to the method. Once you get the specific occurrence of the appointment, you can modify its properties or delete it altogether. If you do this, Outlook creates an Exception object for the appointment and adds this object to the Exceptions collection. You can scroll through the Exceptions collection to locate a specific object, then you can use the AppointmentItem method to return the AppointmentItem object corresponding to that Exception object. The following code sample should clarify this process. It uses the recurring appointment that was created and saved into the Calendar folder in the previous example. Then it opens that appointment and gets a specific occurrence by passing in a date to the GetOccurrence method. It changes the properties on the returned AppointmentItem object, thereby creating an Exception object. The code counts the number of exception objects and displays their names. There should only be only one exception in the collection.
Sub CommandButton1_Click set oNS = Application.GetNameSpace("MAPI") set oCalendar = oNS.GetDefaultFolder(9) set oCal = oCalendar.Items("Status Meetings") set oRecur = oCal.GetRecurrencePattern set oOneOccur = oRecur.GetOccurrence(#10/8/98 3:00 PM#) msgbox "Retrieved the " & oOneOccur.Start & " occurrence." oOneOccur.Start = #10/8/98 5:00 PM# 'Change the occurrence oOneOccur.Save set oRefreshRecur = oCal.GetRecurrencePattern set oExceptions = oRefreshRecur.Exceptions msgbox "There are " & oExceptions.Count & " exceptions." set oFirstException = oExceptions.Item(1) set oNewAppt = oFirstException.AppointmentItem msgbox "The first exceptions start time is " & oNewAppt.Start oNewAppt.Start = #10/7/98 9:00 AM# oNewAppt.Save msgbox "Start time now changed to 9 am" End Sub |
For more information on exceptions and recurring appointments, refer to the Olform.hlp file on the companion CD.