简体   繁体   中英

Filtering Outlook items using C#

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 (ie 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]");

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;
    }
}

https://learn.microsoft.com/en-us/do.net/api/microsoft.office.interop.outlook._items.restrict?view=outlook-pia

That document uses other date Format. I use next filter to check daily for new or edited appointments.

        DateTime filterDate = DateTime.Now.Date.AddDays(-days);
        string filterDateString = filterDate.Day + "/" + filterDate.Month + "/" + filterDate.Year + " 1:00pm";
        string filter = "[LastModificationTime] > '" + string.Format(filterDateString, "ddddd h:nn AMPM") + "'";

        Microsoft.Office.Interop.Outlook.Application oApp = new 
        Microsoft.Office.Interop.Outlook.Application();
        NameSpace nameSpace = oApp.GetNamespace("MAPI");
        MAPIFolder calendarFolder = nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
        Items outlookCalendarItems = calendarFolder.Items.Restrict(filter);
        List<AppointmentItem> appsList = new List<AppointmentItem>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM