Reputation: 1501
I've written a macro which iterates through a users calendar and makes modifications to entries that fufil a certain critera.
The problem is that when the calendar is very big, this takes a long time to do. I don't seem to be able to filter the appointments because oAppointmentItems
seems to store entries as they were created - which is not necessarily the same order as when they start.
The code I'm using is this:
Dim oOL As New Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oAppointments As Object
Dim oAppointmentItem As Outlook.AppointmentItem
Set oNS = oOL.GetNamespace("MAPI")
Set oAppointments = oNS.GetDefaultFolder(olFolderCalendar)
For Each oAppointmentItem In oAppointments.Items
DoEvents
' Something here
Next
Set oAppointmentItem = Nothing
Set oAppointments = Nothing
Set oNS = Nothing
Set oOL = Nothing
Short of removing the DoEvents
(which only means that Outlook appears to lock up to the user) is there any way I can speed this up by applying some kind of filter? For example, appointments which start in the future.
Upvotes: 13
Views: 22983
Reputation: 2788
Hey couldn't get tasks to work but this seem to work on appointments full explaination
Dim myStart As Date
Dim myEnd As Date
myStart = Date
myEnd = DateAdd("d", 30, myStart)
Debug.Print "Start:", myStart
Debug.Print "End:", myEnd
'Construct filter for the next 30-day date range
strRestriction = "[Start] >= '" & _
Format$(myStart, "mm/dd/yyyy hh:mm AMPM") _
& "' AND [End] <= '" & _
Format$(myEnd, "mm/dd/yyyy hh:mm AMPM") & "'"
'Check the restriction string
Debug.Print strRestriction
Const olFolderCalendar = 9
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set oCalendar = olNS.GetDefaultFolder(olFolderTasks)
Set oItems = oCalendar.items
oItems.IncludeRecurrences = True
' oItems.Sort "[Start]" ' commented out worked for me..
'Restrict the Items collection for the 30-day date range
Set oItemsInDateRange = oItems.Restrict(strRestriction)
Debug.Print oItemsInDateRange.Count
Upvotes: 0
Reputation: 91376
You can use Restrict to filter. Note that dates are in the format month, day, year and that they are filtered as strings, even though stored as dates:
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olRecItems = olNS.GetDefaultFolder(olFolderTasks)
strFilter = "[DueDate] > '1/15/2009'"
Set olFilterRecItems = olRecItems.Items.Restrict(strFilter)
For i = 1 To olFilterRecItems.Count
<...>
More information: http://msdn.microsoft.com/en-us/library/bb220369.aspx
Upvotes: 15