2
votes

I am using the Office object model to retrieve my calendar items from Outlook. I want to use the Restrict() method to only get today's appointments. I also want to include the single instance of all recurring appointments (i.e. not all recurrences - just those today).

With the below code, I get many (but not all) recurring items like birthdays regardless of the date. I also get various other appointments - but not those for today.

I've tried different formats for the date, including 2013-07-25 00:00:00, without luck. I've researched the net, and tried to copy examples from VBA scripts - no luck.

Appreciate any ideas from others' who have done this.

var outlook = new Application();
var calendar = outlook.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
DateTime today = DateTime.Today, tomorrow = today.AddDays(1);
const string DateFormat = "dd/MM/yyyy HH:mm";
string filter = string.Format("[Start] >= '{0}' AND [Start] < '{1}'", today.ToString(DateFormat), tomorrow.ToString(DateFormat));
var todaysAppointments = calendar.Items.Restrict(filter);
// todaysAppointments.IncludeRecurrences = true;
todaysAppointments.Sort("[Start]");
1
Have you tried this date format Mddyy h:mm tt? From here: msdn.microsoft.com/en-us/library/office/…Alden
Thanks for the suggestion. I tried this and got rather different results. With IncludeRecurrences=true, I just got recurring appointments - like peoples' birthdays. With that set to false, I just get 3 birthdays and nothing else - and those birthdays aren't even this month...Saqib

1 Answers

0
votes

I used the code below and it works perfectly. I probably use too much 'IncludeRecurrences = false' but it works ;) I had to do that, or it acts weird (I think 'IncludeRecurrences' compares things differently)

Just put the calendar as first parameter and pDateToRead as the date you want. (For example)

var calendar = outlook.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
var calendarItems = GetCalendarItemsOnDate(calendar, DateTime.Today);

Actual method:

public static IEnumerable<Outlook.AppointmentItem> GetCalendarItemsOnDate(this Outlook.MAPIFolder pCalendarFolder, DateTime pDateToRead)
{
    var filter = "( [Start] >= '" + pDateToRead.ToString("MM/dd/yyyy") + "'" + " AND " + "  [End]  < '" +     pDateToRead.AddDays(1).ToString("MM/dd/yyyy") + "' )";
    pCalendarFolder.Items.IncludeRecurrences = false;
    var outlookCalendarItems = pCalendarFolder.Items.Restrict(filter);
    outlookCalendarItems.IncludeRecurrences = false;

    var allItem = string.Empty;
    foreach (Outlook.AppointmentItem item in outlookCalendarItems)
    {
        if (item.IsRecurring)
        {
            continue;
        }
        yield return item;
    }
}