Outlook Calendar OLE Question

Edit: Moved to General in hopes of an answer as no one in Target: Windows wast biting :slight_smile:

Thanks to John Hansen’s example https://forum.xojo.com/5315-addressbook-object-for-windows-outlook-import-expo/0 I have almost refactored my Outlook Calendar code to be much more efficient. I have one niggling problem left.

[code] Dim oResults() As VAD_CalendarEvent

Dim olFolderCalendar As Integer = 9

Dim oApp As OLEObject
oApp  = New OLEObject("Outlook.Application")

Dim oNameSpace As OLEObject
oNameSpace = oApp.GetNameSpace("MAPI")

Dim oRecipient As OLEObject
oRecipient = oNameSpace.CreateRecipient(uName)

Dim oCalendar As OLEObject
oCalendar = oNameSpace.GetSharedDefaultFolder(oRecipient,olFolderCalendar)

Dim oItems As OLEObject 

Dim oItemsInDateRange As OLEObject

Dim oAppt As OLEObject

Dim strRestriction As String

Dim oEvent As VAD_CalendarEvent

strRestriction = "[Start] >= '" + _
dtStart.ShortDate + " " + dtStart.ShortTime _
+ "' AND [End] <= '" + _
dtEnd.ShortDate + " " + dtEnd.ShortTime + "'"

oItems = oCalendar.Items
oItems.IncludeRecurrences = True
oItems.Sort "[Start]"

''Restrict the Items collection for the date range
oItemsInDateRange = oItems.Restrict(strRestriction)

oItemsInDateRange.Sort "[Start]"

 'Dim count As int64 = oItemsInDateRange.Count ' Neither this or below work

 Dim count As int64 = oItemsInDateRange.Value("Count")

For i As Integer = 1 to count
    oAppt = oItemsInDateRange.Item(i)
    oEvent = New VAD_CalendarEvent
    oEvent.sEventName = oAppt.Subject
    oEvent.dtmStart.TotalSeconds = oAppt.Start
    oEvent.dtmEnd.TotalSeconds = oAppt.Start + (oAppt.Duration * 60)
    oEvent.sLocation =oAppt.Location
    oEvent.sDescription = oAppt.Body
    oResults.Append oEvent
  catch OLEerr as OLEException
    'MsgBox OLEerr.Message
  end try

Return oResults[/code]

The problem is the “count” variable. No matter how I try and extract it I get 2147483647 as a value. The actual value is much lower. I can get the appointment information I need but I need to catch an OLEexception to exit the loop.

The exception iis Unknown error:1, (failed on “Item”) - I would expect an out of bounds.

The count property is correct if I do not use " oItems.IncludeRecurrences = True" but I do need the recurring appointments.

Any OLE gurus that can point me to the proper syntax?


You might have a look at this: https://forum.xojo.com/5599-how-to-search-in-outlook-calendar-with-use-of-oleo
maybe this can give you a hint.

That’s all good stuff John. I appreciate it but it doesn’t get me any closer to getting the count property for an item collection when “.IncludeRecurrences = True” Without this I only get the first instance during the range.

I am accessing server shared calendars and need to restrict the data to a date range as iterating over the entire items collection takes forever.

I’ll keep studying!

I got it.
You should not use Count property of Items collection when iterating Items collection with IncludeRecurrence property set to True. The value of Count will be an undefined value.

See http://msdn.microsoft.com/en-us/library/office/aa171434(v=office.11).aspx

Here is an example that follows recurrence

  Dim myOlApp As OLEObject
  Dim myNamespace As OLEObject
  Dim myAppointments As OLEObject
  Dim currentAppointment As OLEObject
  Dim myItems As OLEObject
  // Enumeration See: http://msdn.microsoft.com/en-us/library/office/bb208072%28v=office.12%29.aspx
  Const olFolderCalendar = 9
  myOlApp = NEW OLEObject("Outlook.Application")
  myNamespace = myOlApp.GetNamespace("MAPI")
  myAppointments = myNameSpace.GetDefaultFolder(olFolderCalendar)
  myItems = myAppointments.Items
  myItems.Sort("[Start]", False)
  myItems.IncludeRecurrences = True
  //outText = "Total number of appointments: " + str(nItems) + EndOfLine.Windows + EndOfLine.Windows
  currentAppointment = myItems.Find("[Start] > '12/01/2013'")
  Dim Acount As integer = 0
  While currentAppointment.TypeName <> "Nothing" And Acount < 15
    Acount = Acount +1
    MsgBox ("Count: "+str(Acount)+"  " + currentAppointment.Subject)
    currentAppointment = myItems.FindNext
  myOlApp = Nil
exception err as oleexception
  msgbox err.message

Thank you John. The light has come on. I’m not sure how I missed the info in the link. I appreciate the help.