GetOccurrence Method

Microsoft Outlook Visual Basic

GetOccurrence Method

       

The GetOccurrence method returns a specific instance of the AppointmentItem object on the specified date.

Note  The GetOccurrence method generates an error if no appointment of that series exists on the specified date.

expression.GetOccurrence(StartDate)

expression   Required. An expression that returns one of the objects in the Applies To list.

StartDate  Required Date that represents local time.

Example

This Visual Basic for Applications example uses CreateItem to create an AppointmentItem object. The RecurrencePattern is obtained for this item using the GetRecurrencePattern method. By setting the RecurrencePattern properties, RecurrenceType, PatternStartDate, and PatternEndDate, the appointments are now a recurring series that occur on a daily basis for the period of one year.

An Exception object is created when one instance of this recurring appointment is obtained using the GetOccurrence method and properties for this instance are altered. This exception to the series of appointments is obtained using the GetRecurrencePattern method to access the Exceptions collection associated with this series. Message boxes display the original Subject and OriginalDate for this exception to the series of appointments and the current date, time, and subject for this exception.

For a description of changes required for this example to work in VBScript, see the Note at the end of the example.

Public Sub cmdExample()
    Set myOlApp = New Outlook.Application
    Set myApptItem = myOlApp.CreateItem(olAppointmentItem)   
    myApptItem.Start = #2/2/98 3:00:00 PM#
    myApptItem.End = #2/2/98 4:00:00 PM#
    myApptItem.Subject = "Meet with Boss"
    
    'Get the recurrence pattern for this appointment 
    'and set it so that this is a daily appointment
    'that begins on 2/2/98 and ends on 2/2/99 
     'and save it.
    Set myRecurrPatt = myApptItem.GetRecurrencePattern
    myRecurrPatt.RecurrenceType = olRecursDaily
    myRecurrPatt.PatternStartDate = #2/2/98#
    myRecurrPatt.PatternEndDate = #2/2/99#
    myApptItem.Save
    
    'Access the items in the Calendar folder to locate
    'the master AppointmentItem for the new series.
    Set myNamespace = myOlApp.GetNamespace("MAPI")
    Set myFolder = myNamespace.GetDefaultFolder(olFolderCalendar)
    Set myItems = myFolder.Items
    Set myApptItem = myItems("Meet with Boss")
    
    'Get the recurrence pattern for this appointment 
    'and obtain the occurrence for 3/12/98.
    myDate = #3/12/98 3:00:00 PM#
    Set myRecurrPatt = myApptItem.GetRecurrencePattern
    Set myOddApptItem = myRecurrPatt.GetOccurrence(myDate)
     
    'Save the existing subject. Change the subject and
    'starting time for this particular appointment
    'and save it.
    saveSubject = myOddApptItem.Subject
    myOddApptItem.Subject = "Meet NEW Boss"
    newDate = #3/12/98 3:30:00 PM#
    myOddApptItem.Start = newDate
    myOddApptItem.Save
    
    'Get the recurrence pattern for the master
    'AppointmentItem. Access the collection of
    'exceptions to the regular appointments.
    Set myRecurrPatt = myApptItem.GetRecurrencePattern
    Set myException = myRecurrPatt.Exceptions.Item(1)
    
    'Display the original date, time, and subject
    'for this exception.
    MsgBox myException.OriginalDate & ": " & saveSubject

    'Display the current date, time, and subject
    'for this exception.
    MsgBox myException.AppointmentItem.Start & ": " &
myException.AppointmentItem.Subject
End Sub

Note   For this example to work properly in VBScript, there are only a few simple changes that need to be made in the code.

You don't have to retrieve the application as an object and you must use the values of the constants, so:

Set myOlApp = New Outlook.Application
    Set myApptItem = myOlApp.CreateItem(olAppointmentItem)

becomes:

Set myApptItem = Application.CreateItem(1)

and

myRecurrPatt.RecurrenceType = olRecursDaily

becomes:

myRecurrPatt.RecurrenceType = 0

and

Set myFolder = myNamespace.GetDefaultFolder(olFolderCalendar)

becomes:

Set myFolder = myNamespace.GetDefaultFolder(9)