none
Cannot filter Outlook AppointmentItem with start time range RRS feed

  • Question

  • I try to use some kind of range for the `[Start]`.
    I was given an advice not to use `=` thats why I use `>=` and `<=`:

     var calendar = outlookApplication.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
            
        if (calendar == null || calendar.Items == null)
        {
           return null;
        }
    
        DateTime appointmentStart = new DateTime(2013, 04, 02, 10, 0 ,0);
        
        string filter = string.Format("[Start] >= '{0}' And [Start] <= '{1}'", appointmentStart.AddMinutes(-1).ToString("g"), appointmentStart.AddMinutes(1).ToString("g") );
        var calendarItems = calendar.Items.Restrict(filter);

    This way I cannot find the Appointmen in calendar, but I have one on 02 Apr. 2013 at 10:00:00.

    What is wrong with my code?

    • Edited by MikroDel Wednesday, March 20, 2013 5:53 PM
    Wednesday, March 20, 2013 5:53 PM

Answers

  • i can reproduce your behaviour. No idea why it happens though, sorry. I guess you will have to compensate for it by checking IsRecurring property and then real start time to see if you should omit that item.
    • Marked as answer by MikroDel Thursday, March 21, 2013 10:17 AM
    Thursday, March 21, 2013 10:06 AM

All replies

  • Hi MikroDel,

    I find some helpful link for your question.

    http://stackoverflow.com/questions/8043633/outlook-filter-items-get-all-recurring-appointments-in-a-week-range

    http://stackoverflow.com/questions/1073307/filter-outlook-appointments

    http://stackoverflow.com/questions/90899/net-get-all-outlook-calendar-items/92184#92184

    the three links said you should use CDO to complete your task.

    Thanks

    • Proposed as answer by Learning hard Thursday, March 21, 2013 6:59 AM
    Thursday, March 21, 2013 6:31 AM
  • most probably format of passed date is wrong. First try to hardcode date string format, later switch to .ToString call, something like: Restrict("[Start] >= '02/04/2013 9:59:00'"); . Try to experiment with it and see which date format your Outlook accepts.
    Thursday, March 21, 2013 7:40 AM
  • @Learning hard

    thank you for you answer - I want to try it without CDO :)

    Thursday, March 21, 2013 8:55 AM
  • @DamianD

    Thank you

    I have this code at the moment:

     var calendar = outlookApplication.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
            
        if (calendar == null || calendar.Items == null)
        {
           return null;
        }
    
        DateTime appointmentStart = new DateTime(2013, 03, 25, 10, 0 ,0);
        
        string filter = string.Format("[Start] > '{0}' And [Start] < '{1}'", appointmentStart.AddMinutes(-1).ToString("g"), appointmentStart.AddMinutes(1).ToString("g") );
        var calendarItems = calendar.Items.Restrict(filter);


    And I have an AppointemtItem in Outlook on 25.03.2013 at 10:00:00. So am awaiting to get this AppointmentItem as a result of

    calendar.Items.Restrict(filter);

    but I become 2 AppointmenItems:

    - the correct one

    - and also one from 05.04.2012 - from 05 April 2012 (not 2013) which has Start 10:30:00 and End 10:40:00


    I cannot understand what have 05.04.2012 10:30:00 - 10:40:00 to do with

    filter for > 25.03.2013 09:59 and < 25.03.2013 10:01?


    • Edited by MikroDel Thursday, March 21, 2013 9:10 AM
    Thursday, March 21, 2013 9:01 AM
  • is that one from 2012 a recurring appointment?

    Thursday, March 21, 2013 9:23 AM
  • @DamianD - yes - it is every day in this time
    Thursday, March 21, 2013 9:58 AM
  • i can reproduce your behaviour. No idea why it happens though, sorry. I guess you will have to compensate for it by checking IsRecurring property and then real start time to see if you should omit that item.
    • Marked as answer by MikroDel Thursday, March 21, 2013 10:17 AM
    Thursday, March 21, 2013 10:06 AM
  • IsRecurring check will be enough for me :)

    Thank you for trying to reproduce it )

    • Marked as answer by MikroDel Thursday, March 21, 2013 10:24 AM
    • Unmarked as answer by MikroDel Thursday, March 21, 2013 10:24 AM
    Thursday, March 21, 2013 10:17 AM
  • CDO is obsolete, I would not use it at all anymore. It has tighter security than the Outlook object model, it hasn't been updated in over a dozen years, and it cannot be used from any managed code at all due to the memory model.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Learning hard" <=?utf-8?B?TGVhcm5pbmcgaGFyZA==?=> wrote in message news:ecb72ce6-2427-49a6-8d1d-a6dc1dc01554...

    Hi MikroDel,

    I find some helpful link for your question.

    http://stackoverflow.com/questions/8043633/outlook-filter-items-get-all-recurring-appointments-in-a-week-range

    http://stackoverflow.com/questions/1073307/filter-outlook-appointments

    http://stackoverflow.com/questions/90899/net-get-all-outlook-calendar-items/92184#92184

    the three links said you should use CDO to complete your task.

    Thanks


    Ken Slovak MVP - Outlook
    Thursday, March 21, 2013 2:49 PM
    Moderator
  • In general you want to filter/restrict any time values based on not using any seconds and using a time range. Due to round-off you rarely have an exact time match, you usually would filter on >= (some time) && <= (some other time) to get what you need.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "MikroDel" <=?utf-8?B?TWlrcm9EZWw=?=> wrote in message news:2828ad18-5c4e-4288-bf51-1ed84e0bdba2...

    IsRecurring check will be enough for me :)

    Thank you for trying to reproduce it )


    Ken Slovak MVP - Outlook
    Thursday, March 21, 2013 2:49 PM
    Moderator