Retrieving Recurring Appointments in Outlook 2007
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
Summary: Learn how to retrieve recurring appointments in Microsoft Office Outlook 2007 by using the Outlook object model. The solution requires running a short macro.
Applies to: Microsoft Office Outlook 2007
When you are writing programming code that retrieves a set of calendar appointments in Microsoft Office Outlook 2007, recurring appointments are not automatically included in the collection of appointment items. There is an IncludeRecurrences property of the Items object that you can use to include recurring appointments.
In Outlook 2007, you can create a recurring appointment (for example, a meeting that occurs at 1:00 P.M. every Monday for the next six months). If you want to programmatically filter calendar items and include recurring appointments in your results, you need to set the collection’s IncludeRecurrences property to True in your code.
To properly filter calendar items to include recurring appointments, use the following procedure.
To filter calendar items to include recurring appointments
Assign the set of appointment items in your calendar to an Items object.
You can use the Items object to represent any set of items in Outlook (for example, mail items, appointment items, and so on). This step allows you to subsequently use the IncludeRecurrences property of the Items object.
Sort the set of appointments by the start date in ascending order.
This step is necessary so that the IncludeRecurrences property will work properly.
Set the IncludeRecurrences property of the Items object that you obtained in step 1 to True.
Include in your filter a date range for the appointments you want to retrieve. All appointments that you want to retrieve must begin in this date range.
Programmatically, you can specify a Jet query that filters by using the Find method of the Items object. You must include a date-range restriction in your filter; otherwise, a recurring series of appointments that does not have an end date will return erroneous filter results.
In step 3, if you set IncludeRecurrences to True, the Items object will contain both simple nonrecurring appointments and recurring appointments, including the master appointment (also known as the series). If you set IncludeRecurrences to False (the default), the Items object will consist of only simple nonrecurring appointments and the master appointment for any recurring appointments.
For example, assume that a recurring appointment was originally created on the first Monday in January, and the appointment was set to recur every Monday thereafter. If you do not use the IncludeRecurrences property, the code will return only the first appointment in January. If you try to retrieve only the appointments in February, the code will not return any of the recurring appointments.
The following Visual Basic for Applications (VBA) example filters simply for appointments that start today or tomorrow, including recurring appointments, and displays the subject of each appointment.
Sub DemoFindNext() Dim myNameSpace As Outlook.NameSpace Dim tdystart As Date Dim tdyend As Date Dim myAppointments As Outlook.Items Dim currentAppointment As Outlook.AppointmentItem Set myNameSpace = Application.GetNamespace("MAPI") tdystart = VBA.Format(Now, "Short Date") tdyend = VBA.Format(Now + 1, "Short Date") Set myAppointments = myNameSpace.GetDefaultFolder(olFolderCalendar).Items myAppointments.Sort "[Start]" myAppointments.IncludeRecurrences = True Set currentAppointment = myAppointments.Find("[Start] >= """ & _ tdystart & """ and [Start] <= """ & tdyend & """") While TypeName(currentAppointment) <> "Nothing" MsgBox currentAppointment.Subject Set currentAppointment = myAppointments.FindNext Wend End Sub
The default macro security setting is Warnings for signed macros; all unsigned macros are disabled. This setting allows a macro to run without any security prompts if the macro is digitally signed by a trusted publisher. All unsigned macros are disabled without notification. Therefore, unless your code sample is signed by a trusted publisher, you will not be able to run the code sample with this security setting. For more information about trusted publishers, see Add, remove, or view a trusted publisher.
The code sample in this article is not digitally signed. Therefore, you should set macro security to the Warnings for all macros option to run just this code sample. After changing to this option, restart Outlook. The Warnings for all macros option initially disables all macros. The first time you attempt to run a macro or start the Visual Basic Editor, you will see the Microsoft Office Outlook Security Notice dialog box. Click Enable Macros.
Because the Warnings for signed macros; all unsigned macros are disabled security setting provides a higher level of security for Outlook than the Warnings for all macros setting, you should set your macro security to Warnings for signed macros; all unsigned macros are disabled after you run this code sample. Restart Outlook for this setting to take effect.
To change the macro security setting
On the Tools menu, click Trust Center.
Click Macro Security.
Select Warnings for all macros.
To run the code sample as an Outlook macro
In Outlook 2007, press ALT+F11 to start the Visual Basic Editor.
If this is the first time you have started the Visual Basic Editor in the current Outlook session, you will see the Microsoft Office Outlook Security Notice dialog box. Click Enable Macros.
In the Project Explorer, expand the Project1 node.
Expand the Microsoft Office Outlook Objects node.
On the Tools menu, click References.
In the References dialog box, ensure that Microsoft Outlook 12.0 Object Library and Visual Basic for Applications are selected.
Click OK to close the References dialog box.
To create the macro, copy and paste the preceding code sample into the Code window.
Press F5 to run the macro.
Close the Visual Basic Editor.
To reset macro security after running this sample
On the Tools menu, click Trust Center.
Click Macro Settings.
Select Warnings for signed macros; all unsigned macros are disabled.