none
Problem in filtering Outlook Appointments using Restrict command on Target Machine RRS feed

  • Question

  • I have .Net (Visual Studio 2010 / VB) program that reads appointments within a date-range. The program works correctly on my development machine which has Outlook 2007, but on the target machine (which has Outlook 2010), I’m getting all of the appointments (the .Restrict statement has no effect).

    I then wrote a small VBA macro that does the same thing...the .Restrict statement works as designed and it filters out the appointments by the specified date-range.

    I know this is a VBA forum, but I’m not getting any assistance from the Visual Basic forum...so I’m asking it here.

    Here is the VBA code (which works):

    Sub ListAllAppts()
    
      Dim apptCurrent As AppointmentItem
      Dim fldrCalendar As Folder
      Dim oItems As Items
      Dim oItemsInDateRange As Items
      Dim strRestriction As String
    
      strRestriction = "[Start] >= '7/26/2015 12:00 AM' AND [End] <= '9/4/2015 11:59 PM'"
      
    ' This is the initial reference to an appointment collection.
      Set fldrCalendar = Outlook.Application.Session.GetDefaultFolder(olFolderCalendar)
      Set oItems = fldrCalendar.Items
      oItems.IncludeRecurrences = True
      oItems.Sort "[Start]"
      Set oItemsInDateRange = oItems.Restrict(strRestriction)
      For Each apptCurrent In oItemsInDateRange
          Debug.Print apptCurrent.Start & " " & apptCurrent.Subject & " " & apptCurrent.RecurrenceState
      Next apptCurrent
    
    End Sub
    

    Here is the Visual Basic code (it’s a test form that has a Listbox...the routine populates the Listbox):

    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
        Dim myOutlookApp As Outlook.Application
        Dim myOutlookCalendar, myOutlookFolder As Outlook.MAPIFolder
        Dim itemsCalendar As Outlook.Items
        Dim myAppointment As Outlook.AppointmentItem
        Dim dteStart, dteEnd As Date
        Dim strFilter As String
    
        Me.lboAppts.Items.Clear()
        dteStart = Me.txtDateFrom.Text
        dteEnd = Me.txtDateTo.Text
    
        dteEnd = dteEnd.AddHours(23)
        dteEnd = dteEnd.AddMinutes(59)
        dteEnd = dteEnd.AddSeconds(59)
        strFilter = "[Start] >= '" + dteStart.ToString("g") + "' AND [End] <= '" + dteEnd.ToString("g") + "'"
        'Display the strFilter field back on the form for debugging
        me.txtFilterString.text = strFilter)
    
        myOutlookApp = New Outlook.Application
        If (fncGetOutlookApp(myOutlookApp)) Then
        Else
          Exit Sub
        End If
    
        For Each myOutlookFolder In myOutlookApp.Session.Folders
          For Each myOutlookCalendar In myOutlookFolder.Folders
            If (myOutlookCalendar.DefaultItemType = Outlook.OlItemType.olAppointmentItem) Then
              itemsCalendar = myOutlookCalendar.Items.Restrict(strFilter)
              itemsCalendar.IncludeRecurrences = True
              itemsCalendar.Sort("[Start]", Type.Missing)
              For Each myAppointment In itemsCalendar
                'Debug.Print(myAppointment.Start & " | " & myAppointment.Subject)
                Me.lboAppts.Items.Add(myAppointment.Start & " | " & myAppointment.Subject)
              Next
            End If
          Next
        Next
      End Sub
    
      Private Function fncGetOutlookApp(parmOutlookApp As Outlook.Application) As Boolean ' Outlook.Application
    
        If (Process.GetProcessesByName("Outlook").Count > 0) Then
          Try
            parmOutlookApp = DirectCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)
          Catch ex As COMException
            Return False
            Exit Function
          End Try
        Else
          Try
            parmOutlookApp = New Outlook.Application
            Dim nsOutlook As Outlook.NameSpace
            nsOutlook.Logon("", "")
            nsOutlook = Nothing
          Catch ex As COMException
            Return False
            Exit Function
          End Try
        End If
    
        Return True
    
      End Function
    End Class

    I added a switch into the Visual Basic which checks if the AppointmentItem.Start is outside of the date range. If an item is found, then the switch is turned on and displayed after reading all of the items. I’m getting the switch turned on. I can use this option to check each AppointmentItem, but it sure will slow the program down...and obviously it’s what the .Restrict command is supposed to do.

    Thank you for your time in advance.


    Paul D. Goldstein Forceware Systems, Inc.

    Wednesday, August 12, 2015 9:13 PM

Answers

  • Hello Paul,

    You get a new instance of the Items each time you call the Items property. Try to use the following code instead:

     itemsCalendar = myOutlookCalendar.Items
     itemsCalendar.IncludeRecurrences = True
     itemsCalendar.Sort("[Start]", Type.Missing)
     filteredItems = itemsCalendar.Restrict(strFilter)

    Note, you need to call the Sort method before the Restrict one. 

    Take a look at the How To: Use Restrict method in Outlook to get calendar items article which describes the Restrict method of the Items class in action and provides the sample code in VB.NET.
    Wednesday, August 12, 2015 9:31 PM

All replies

  • Hello Paul,

    You get a new instance of the Items each time you call the Items property. Try to use the following code instead:

     itemsCalendar = myOutlookCalendar.Items
     itemsCalendar.IncludeRecurrences = True
     itemsCalendar.Sort("[Start]", Type.Missing)
     filteredItems = itemsCalendar.Restrict(strFilter)

    Note, you need to call the Sort method before the Restrict one. 

    Take a look at the How To: Use Restrict method in Outlook to get calendar items article which describes the Restrict method of the Items class in action and provides the sample code in VB.NET.
    Wednesday, August 12, 2015 9:31 PM
  • Hi Paul,

    What's the value of Me.txtDateFrom.Text and Me.txtDateTo.Text?

    As far as I test, the code works well for me both in Outlook 2010 and Outlook 2013. Here is the modified code for your reference:

      Private Sub btnGetAppointment_Click(sender As Object, e As EventArgs) Handles btnGetAppointment.Click
            Dim myOutlookApp As Outlook.Application
            Dim myOutlookCalendar, myOutlookFolder As Outlook.MAPIFolder
            Dim itemsCalendar As Outlook.Items
            Dim myAppointment As Outlook.AppointmentItem
            Dim dteStart, dteEnd As Date
            Dim strFilter As String
    
            'Me.lboAppts.Items.Clear()
            'dteStart = Me.txtDateFrom.Text
            'dteEnd = Me.txtDateTo.Text
    
            dteStart = DateTime.Today'modify the date
            dteEnd = DateTime.Today'modify the date
    
    
            dteEnd = dteEnd.AddHours(23)
            dteEnd = dteEnd.AddMinutes(59)
            dteEnd = dteEnd.AddSeconds(59)
            strFilter = "[Start] >= '" + dteStart.ToString("g") + "' AND [End] <= '" + dteEnd.ToString("g") + "'"
            'Display the strFilter field back on the form for debugging
            'me.txtFilterString.text = strFilter)
    
            myOutlookApp = New Outlook.Application
            If (fncGetOutlookApp(myOutlookApp)) Then
            Else
                Exit Sub
            End If
            lblAppointmentList.Text = ""
            For Each myOutlookFolder In myOutlookApp.Session.Folders
                For Each myOutlookCalendar In myOutlookFolder.Folders
                    If (myOutlookCalendar.DefaultItemType = Outlook.OlItemType.olAppointmentItem) Then
                        itemsCalendar = myOutlookCalendar.Items.Restrict(strFilter)
                        itemsCalendar.IncludeRecurrences = True
                        itemsCalendar.Sort("[Start]", Type.Missing)
    
                        For Each myAppointment In itemsCalendar
                            Debug.Print(myAppointment.Start & " | " & myAppointment.Subject)
                            lblAppointmentList.Text = lblAppointmentList.Text & myAppointment.Start & " | " & myAppointment.Subject & vbCrLf
                            'Me.lboAppts.Items.Add(myAppointment.Start & " | " & myAppointment.Subject)
                        Next
                    End If
                Next
            Next
    
        End Sub
    Regards & Fei




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 14, 2015 5:09 AM
    Moderator
  • Hey Eugene,

    That solved it. I guess the question I should have been asking instead if it’s working on my development machine then why not on the target...is: “It it’s not working on my target machine, then why does it work on my development machine?”

    Very strange. In other words...it never should have worked on the development machine...I would have debugged it months ago.

    Thanks again for your assistance.


    Paul D. Goldstein Forceware Systems, Inc.

    Friday, August 14, 2015 1:42 PM
  • Hi Fei,

    If you read my above response to Eugene, you’ll see that his solution (.Restrict should come after the .IncludeRecurrences and .Sort statement) is correct.

    If your code is working...it’s not supposed to.

    You’ll see in my VBA code (which I wrote on the target machine without access to the code on the development machine, I didn’t realize that I did just that...put the .Restrict statement last).

    Thanks for your spending time trying to assist me. I really appreciate it.


    Paul D. Goldstein Forceware Systems, Inc.

    Friday, August 14, 2015 1:48 PM