You might find the information at
http://www.outlookcode.com/d/finddate.htm
useful. It suggests some refinements.
I wish I had known about Find and Restrict earlier
The MS documentation says: "The Restrict method is significantly faster if
there is a large number of items in the collection, especially if only a few
items in a large collection are expected to be found."
(
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaol11/ht
ml/olmthRestrict.asp)
But I'm kind of lost trying to put Restrict into action along with
IncludeRecurrences.
What I'm trying to do is integrate the OL calendar with an Access Contact
Management Database (mdb). The idea is to allow users to set, get, update
and delete appointments from within Access. This provides automatic
association of appointments with contacts in the mdb.
One of the challenges in doing this is finding a way to differentiate
"Access" appointments from "Outlook" appointments - so users can still use
the OL calendar as normal but can easily recognize appointments that "belong
to the database", so to speak. So what I did was use the Location field to
store the Entity_ID of the database record. I could have used a different
field, but Location was clearly visible on the default Outlook appointment
form (I wanted to avoid custom forms); the downside, obviously, is that you
cannot use the Location field.
In any case, I have code that adds appointments, and also code that syncs up
the database with outlook. Here's the (abbreviated) code that *gets* the
appointments when the sync code is run:
Dim ola As Outlook.AppointmentItem
For Each ola In olns.GetDefaultFolder(olFolderCalendar).Items
If ola.Start >= (myStartDate) And ola.Start <= (myEndDate) Then
If Not IsNumeric(ola.Location) Or IsNull(ola.Location)
Then
varAloc = 0
'sets Entity_ID to 0 so the database will know this is
an "Outlook" appointment
Else
varAloc = ola.Location 'this is assumed to be an
Entity_ID
End If
With rstAppt
.AddNew
!Entity_ID = varAloc
If Format(TimeValue(CDate(ola.Start)), "Medium
Time") <> "12:00 AM" _
Then !ApptTime =
Format(TimeValue(CDate(ola.Start)), "Medium Time")
!ApptDate = Format(DateValue(CDate(ola.Start)),
"Short Date")
!ApptDay = Format(DateValue(CDate(ola.Start)),
"ddd")
!Subject = ola.Subject
.Update
End With
End If
Next
The problem is: 1) recurring appointments are missed, and 2) it's slow -
since it has to loop through all appointment items in the OL calendar.
Now let's try it with the Restrict Method:
Dim ola As Outlook.AppointmentItem
Dim olaRcr As Outlook.AppointmentItem
Dim oli As Outlook.Items
Dim oliRcr as Outlook.Items
Dim oliRct As Outlook.Items
Dim rcrAppt As Outlook.Items
Dim objItem As Object
Set olir = olns.GetDefaultFolder(olFolderCalendar).Items
Set oli = oliRct.Restrict("[Location] Like '#'") '?????
'can I use more than one Restrict criteria?
oli.IncludeRecurrences.True
For Each objItem in oli
If objItem.IsRecurring Then
Set oliRcr = olns.GetDefaultFolder(olFolderCalendar).Items
Set oliRcr.IncludeRecurrences = True
Set olaRcr = oliRcr.Find("[Start] >= " & Chr(34) & _
myStartDate & " and [Start] <= " & Chr(34) & myEndDate & Chr(34))
While TypeName(olaRcr) <> "Nothing" [add to Access
recordset here]
Set currentAppointment = myAppointments.FindNext
Wend
Else
[add to Access recordset here]
End If
Next
My question is this: How do I Restrict on more than one criteria? I'm only
interested in the appointment if Location IsNumeric and if [Start] is
between myStartDate and myEndDate. Also, am I handling the extraction of
recurring appointments properly by putting the While loop inside the For
Each loop?
If I set the Restrict criteria first, will I still need the Find criteria?
For example, would something like this work:
Set olir = olns.GetDefaultFolder(olFolderCalendar).Items
Set oli = oliRct.Restrict("IsNumeric([Location]) And _
[Start] >= " & Chr(34) & myStartDate & " and [Start] <= " & _
Chr(34) & myEndDate & Chr(34))
'now the set is narrowed, so no While loop is needed (?)
oli.IncludeRecurrences.True
For Each objItem in oli
[add to Access recordset here]
Next
Thanks in advance for your help.