AppointmentItem Object (Outlook)
Published: July 16, 2012
Represents a meeting, a one-time appointment, or a recurring appointment or meeting in the Calendar folder.
Use the CreateItem method to create an AppointmentItem object that represents a new appointment.
Use Items (index), where index is the index number of an appointment or a value used to match the default property of an appointment, to return a single AppointmentItem object from a Calendar folder.
You can also return an AppointmentItem object from a MeetingItem object by using the GetAssociatedAppointment method.
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 for Applications (VBA) or 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.
The following code example in VBA shows how to release and refresh references in order to obtain up-to-date data for a recurring appointment. The example obtains a set of appointment items from the Calendar folder. It assumes that the first item in the appointment collection is part of a recurring appointment. The example shows that a reference to the appointment collection obtained before an exception is created does not reflect the exception. The example then releases this reference and other existing appointment references, after which new references that point to the appointment collection reflect the exception.
Sub TestExceptions() Dim oItems As Items Dim oItemOriginal As AppointmentItem Dim oItemNew As AppointmentItem Dim rPattern As RecurrencePattern Dim oEx As Exceptions Dim oEx2 As Exceptions Dim oOccurrence As AppointmentItem Dim i As Long ' This is the initial reference to an appointment collection. Set oItems = _ Outlook.Application.Session.GetDefaultFolder(olFolderCalendar).Items ' This is the original reference to the first appointment in the ' collection before an exception is created. Set oItemOriginal = oItems.Item(1) ' Code example assumes that the first appointment in the collection ' is a recurring appointment. Set oOccurrence = _ oItemOriginal.GetRecurrencePattern().GetOccurrence(#2/28/2010 8:00:00 AM#) ' Create an exception by changing the 2/28 occurrence to 3/3. oOccurrence.Start = #3/3/2010 8:00:00 AM# oOccurrence.Save Stop ' Preexisting reference to the first appointment in the collection ' does not reflect the exception. oItemOriginal.Save Set oEx = oItemOriginal.GetRecurrencePattern().Exceptions Debug.Print oItemOriginal.subject Debug.Print " Original item exceptions: " & oEx.Count ' Get a new reference based on the existing reference to the ' appointment collection created before the exception. ' The new reference does not reflect the exception. Set oItemNew = oItems.Item(1) oItemNew.Save Set oEx2 = oItemNew.GetRecurrencePattern().Exceptions Debug.Print " New item exceptions: " & oEx2.Count ' Same: preexisting reference to the first appointment in the collection ' does not reflect the exception. Set oEx = oItemOriginal.GetRecurrencePattern().Exceptions Debug.Print " Original item exceptions: " & oEx.Count ' Release all existing references to appointment items, ' including the appointment collection, an exception, occurrence, ' or any other appointment. Debug.Print "REFRESH ITEM COLLECTION" Set oItems = Nothing Set oItemNew = Nothing Set oEx = Nothing Set oEx2 = Nothing Set oOccurrence = Nothing Set oItemOriginal = Nothing Set rPattern = Nothing ' Get new references to appointment items, including the appointment ' collection, individual appointments, and exceptions. Set oItems = _ Outlook.Application.Session.GetDefaultFolder(olFolderCalendar).Items Set oItemNew = oItems.Item(1) ' If no other add-ins have the same recurring appointment open, ' the new references reflect the current exception count. Set oEx2 = oItemNew.GetRecurrencePattern().Exceptions Debug.Print " New item exceptions: " & oEx2.Count Debug.Print "RE-GET ORIGINAL" Set oItemOriginal = oItems.Item(1) Set oEx = oItemOriginal.GetRecurrencePattern().Exceptions Debug.Print " Original item exceptions: " & oEx.Count End Sub