Datetime filter for search not working properly RRS feed

  • General discussion

  • When working with outlook advanced search and outlook Restrict function, it seems that the time filter don't working correctly.

    Private Sub FilterItems2()
        Dim Session As Outlook.NameSpace
        Dim Report As String
        Dim AppointmentsFolder As Outlook.Folder
        Dim myItems As Outlook.Items
        Dim myRestrictItems As Outlook.Items
        Dim currentItem As Object
        Dim currentAppointment As AppointmentItem
        Set Session = Application.Session
        Set AppointmentsFolder = Session.GetDefaultFolder(olFolderCalendar)
        Dim sFilter As String
        'sFilter = "[LastModificationTime] > '4/24/2015 5:01:00 PM'" 'Not work
        sFilter = "[LastModificationTime] > '4/24/2015 5:01 PM'"
        Set myFolder = AppointmentsFolder
        Set myItems = myFolder.Items
        Set myRestrictItems = myItems.Restrict(sFilter)
         For Each myItem In myRestrictItems
            Debug.Print myItem.Subject & vbCrLf
    End Sub
    Hope it can help somebody else.

    Friday, April 24, 2015 9:55 AM

All replies

  • If you are working with .NET, stop formatting as ToString("G"), try to format as .ToString("MM/dd/yyyy hh:mm tt").

    Friday, April 24, 2015 10:00 AM
  • Hello Dummy,

    The Restrict method description in MSDN states the following:

    To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. The following example creates a filter to find all contacts that have been modified after April 24, 2015 at 5:01 P.M. 

    sFilter = "[LastModificationTime] > '" & Format("4/24/2015 5:01pm", "ddddd h:nn AMPM") & "'"

    Friday, April 24, 2015 10:03 AM
  • Yep. Format() method in VBA will work.
    But the fact is that I'm developing the addin using C#.
    I guess I've no other choice.
    Friday, April 24, 2015 10:18 AM
  • There is no problems to format the string in .Net applications. See the Format method of the System.String class for more information. Or just use the ToString method.
    Friday, April 24, 2015 10:20 AM
  • Yep. I know how to format a datetime object to string.

    In the msdn documentation, it mentions that we can format that use "g".

    You can refer to the following page.

    In C#, MyDateTime.ToString("g") will not filter the expected result.

    Friday, April 24, 2015 10:30 AM
  • I see entirely different answers:

    > Yep. It will work.

    > It does not work.

    Could you be more specific?

    It looks like the VBA macro works correctly, but not in C#. Am I right?

    If so, try to check the result strings. What filter strings do you have in VBA and C#?

    Friday, April 24, 2015 11:42 AM
  • Thank you for your reply.

    I've edited my last two post if you'd like to see.

    In fact, I've already got the answer before asking the question. I'm coming to share my experience.

    If you are working with VBA, use the built-in format method for Datetime type.

    If you are working with C#, stop formatting as ToString("G") or ToString("g"),, try to format as .ToString("MM/dd/yyyy hh:mm tt"). 

    Wednesday, May 13, 2015 9:38 AM