Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating quickly through Outlook appointment items

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.

like image 547
Richard Avatar asked Dec 18 '09 11:12

Richard


1 Answers

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

like image 192
Fionnuala Avatar answered Nov 09 '22 11:11

Fionnuala