views:

112

answers:

1

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.

+2  A: 

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

Remou
This was EXACTLY what I was looking for today! This has saved me so much trouble. One thing I notice though is that I can't get it to work with a date filter using =, and it's hard to get an exact date (seems to depend on whether they're a date or a date/time in Outlook). Like > Date - 1 day and < Date will return one with just Date, whereas Date > Date And Date < Date + 1 day will return the items with Date/Time
Jeff