How To Search In Outlook Calendar With Use Of OLEObject

I just had a little bit of time to make some more examples how to get the entries from the Outlook 2010 Calendar folder.
I used the methods and properties from Outlook 2003, so it might work from version Outlook 2003.

Example 1:

 'This example gets all appointments from default Outlook calendar and display only the first three appointments.
  
  Dim myOlApp As OLEObject
  Dim myNamespace As OLEObject
  Dim myAppointments As OLEObject
  Dim myItems As OLEObject
  Dim myItem As OLEObject
  
  Dim nItems As Integer
  
  Dim outText As String
  
  Dim CalDate As New Date   // used for Calendar Start and End Time
  Dim CalStartTime As String 
  Dim CalEndTime As String 
  Dim CalDuration As String
  Dim CalAllDayEvent As String
  Dim CalRecurrence As String
  
  // 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
  nItems = myItems.count
  
  outText = "Total number of appointments: " + str(nItems) + EndOfLine.Windows + EndOfLine.Windows
  
  If nItems > 3 Then nItems = 3   // in this case we only want to show max 3 appointments
  
  For i as integer = 1 to nItems
    myItem = myAppointments.Items(i)
    
    CalDate.TotalSeconds = myItem.invoke("Start")
    CalStartTime = str(CalDate)
    
    CalDate.TotalSeconds = myItem.invoke("End")  // Can't use syntax: myItem.End. Conflict with Xojo
    CalEndTime  = str(CalDate)
    
    CalDuration = str(myitem.Invoke("Duration"))
    
    if myitem.AllDayEvent Then
      CalAllDayEvent = "Yes"
    Else
      CalAllDayEvent = "No"
    End If
    
    if myItem.IsRecurring Then
      CalRecurrence = "Yes"
    Else
      CalRecurrence = "No"
    End If
    
    outText = outText + "Appointment: " + str(i) +EndOfLine.Windows _
    + "Subject: "+ myItem.Subject + EndOfLine.Windows _
    + "Location:  " + myItem.Location + EndOfLine.Windows _
    + "Start time:  " + CalStartTime + EndOfLine.Windows _
    + "End time:  "  + CalEndTime + EndOfLine.Windows _
    + "Duration:  " + CalDuration + " Minutes" +EndOfLine.Windows _
    + "All day Event: "+ CalAllDayEvent +EndOfLine.Windows _
    + "Recurrence: " + CalRecurrence + EndOfLine.Windows _
    + "Text: " + Left(myItem.Body, 80) +EndOfLine.Windows +EndOfLine.Windows  // Only print max 80 characters out from the body
    
    // Appointment Property  info:  http://msdn.microsoft.com/en-us/library/office/aa210899%28v=office.11%29.aspx
  Next
  
  Msgbox outText
  
  myOlApp = Nil
  
exception err as oleexception
  msgbox err.message

Example 2:

  'This example gets all appointments from default Outlook calendar 
  'and display only the first three appointments from search query
  
  
  Dim myOlApp As OLEObject
  Dim myNamespace As OLEObject
  Dim myAppointments As OLEObject
  Dim myItems As OLEObject
  Dim myItem As OLEObject
  Dim myRestrictItems As OLEObject
  
  
  Dim nItems, nRestrictItems As Integer
  
  Dim outText As String
  
  Dim CalDate As New Date   // used for Calendar Start and End Time
  Dim CalStartTime As String
  Dim CalEndTime As String
  Dim CalDuration As String
  Dim CalAllDayEvent As String
  Dim CalRecurrence As String
  
  // 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
  nItems = myItems.count
  
  // Restrict method: http://msdn.microsoft.com/en-us/library/office/aa210275%28v=office.11%29.aspx
  
  //search query
  //myRestrictItems = myItems.Restrict("[LastModificationTime] > '11/10/2013'")
  myRestrictItems = myItems.Restrict("[Start] > '12/10/2013'")
  nRestrictItems = myRestrictItems.count
  
  outText = "Total number of appointments: " + str(nItems)  +"  Restrict appointments: "+ str(nRestrictItems)+ EndOfLine.Windows + EndOfLine.Windows
  
  If nRestrictItems > 3 Then nRestrictItems = 3   // in this case we only want to show max 3 appointments from Search query above
  
  For i as integer = 1 to nRestrictItems
    myItem = myAppointments.Items(i)
    
    CalDate.TotalSeconds = myItem.invoke("Start")
    CalStartTime = str(CalDate)
    
    CalDate.TotalSeconds = myItem.invoke("End") // Can't use syntax: myItem.End. Conflict with Xojo
    CalEndTime  = str(CalDate)
    
    CalDuration = str(myitem.Invoke("Duration"))
    
    if myitem.AllDayEvent Then
      CalAllDayEvent = "Yes"
    Else
      CalAllDayEvent = "No"
    End If
    
    if myItem.IsRecurring Then
      CalRecurrence = "Yes"
    Else
      CalRecurrence = "No"
    End If
    
    outText = outText + "Appointment: " + str(i) +EndOfLine.Windows _
    + "Subject: "+ myItem.Subject + EndOfLine.Windows _
    + "Location:  " + myItem.Location + EndOfLine.Windows _
    + "Start time:  " + CalStartTime + EndOfLine.Windows _
    + "End time:  "  + CalEndTime + EndOfLine.Windows _
    + "Duration:  " + CalDuration + " Minutes" +EndOfLine.Windows _
    + "All day Event: "+ CalAllDayEvent +EndOfLine.Windows _
    + "Recurrence: " + CalRecurrence + EndOfLine.Windows _
    + "Text: " + Left(myItem.Body, 80) +EndOfLine.Windows +EndOfLine.Windows  // Only print max 80 characters out from the body
    
    // Appointment Property  info:  http://msdn.microsoft.com/en-us/library/office/aa210899%28v=office.11%29.aspx
    
  Next
  
  Msgbox outText
  
  myOlApp = Nil
  
exception err as oleexception
  msgbox err.message

INFO
If you are in doubt what variable type the object returns. You can always do a test like these examples as shown:

Here myItem.invoke(“Start”) returns variable type “Double”

    Dim myVarType1 As variant
    myVarType1 =  myItem.invoke("Start")
    msgbox str(myVarType1.Type)   //Display 5  which is a variable type "Double"

Here myItem.Start returns variable type “Object”

    Dim myVarType2 As variant
    myVarType2 =  myItem.Start
    msgbox str(myVarType2.Type)   //Display 9  which is a variable type "Object"

Just using different syntax: myItem.invoke(“Start”) or myItem.Start you get different value types returned, and sometimes this can create errors in your code.

According to the Microsoft documentation " myItem.Start" will return a variable type “double”, so I should be able to write this:

msgbox str(myItem.Start)

Unfortunately this will create an error

But if you write it like this:

  Dim myD as Double = myItem.Start
  msgbox str(myD)

Then it works fine

Links to previous Examples using OLEObject in windows:

  1. https://forum.xojo.com/5315-addressbook-object-for-windows-outlook-import-expo
  2. https://forum.xojo.com/4447-shell-issue
  3. https://forum.xojo.com/4693-write-an-openoffice-libreoffice-document-with-use-
  4. https://forum.xojo.com/5175-ssh-telnet-control

Hello,

Thanks for this code, it work fine.

Do you have found a solution to get GetRecurrencePattern ? This VBA script take only the first appointment for recurence.

I try to get it with this example, but I can’t achieve.

Anyone have an idea ?

Regards