.NET:获取所有Outlook日历项目

时间:2020-03-06 14:20:23  来源:igfitidea点击:

如何从特定日历(特定日期)中获取所有项目。
比方说,例如,我每个星期一晚上都有一个带有重复项目的日历。当我要求所有此类物品时:

CalendarItems = CalendarFolder.Items;
CalendarItems.IncludeRecurrences = true;

我只有一件...

有没有一种简单的方法可以从日历中获取所有项目(主要项目+派生项目)?
在我的特定情况下,可以设置日期限制,但是仅获取所有项目(我的重复项目本身受时间限制)会很酷。

我正在使用Microsoft Outlook 12对象库(Microsoft.Office.Interop.Outlook)。

解决方案

我相信我们必须限制或者查找才能获得定期约会,否则Outlook不会扩展它们。另外,在设置IncludeRecurrences之前,我们必须按"开始"排序。

我研究了文档,这是我的结果:
我将硬编码的期限设置为一个月,但这仅是示例。

public void GetAllCalendarItems()
{
    Microsoft.Office.Interop.Outlook.Application oApp = null;
    Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
    Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null;
    Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null;

    oApp = new Microsoft.Office.Interop.Outlook.Application();
    mapiNamespace = oApp.GetNamespace("MAPI"); ;
    CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
    outlookCalendarItems = CalendarFolder.Items;
    outlookCalendarItems.IncludeRecurrences = true;

    foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
    {
        if (item.IsRecurring)
        {
            Microsoft.Office.Interop.Outlook.RecurrencePattern rp = item.GetRecurrencePattern();
            DateTime first = new DateTime(2008, 8, 31, item.Start.Hour, item.Start.Minute, 0);
            DateTime last = new DateTime(2008, 10, 1);
            Microsoft.Office.Interop.Outlook.AppointmentItem recur = null;

            for (DateTime cur = first; cur <= last; cur = cur.AddDays(1))
            {
                try
                {
                    recur = rp.GetOccurrence(cur);
                    MessageBox.Show(recur.Subject + " -> " + cur.ToLongDateString());
                }
                catch
                { }
            }
        }
        else
        {
            MessageBox.Show(item.Subject + " -> " + item.Start.ToLongDateString());
        }
    }

}

calendarFolder = 
    mapiNamespace.GetDefaultFolder(
        Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);