【问题标题】:Filtering Outlook items using C#使用 C# 过滤 Outlook 项目
【发布时间】:2013-07-25 23:49:05
【问题描述】:

我正在使用 Office 对象模型从 Outlook 中检索我的日历项目。我想使用 Restrict() 方法只获取今天的约会。我还想包括所有定期约会的单个实例(即并非所有重复 - 只是今天的那些)。

使用以下代码,无论日期如何,我都会收到许多(但不是全部)重复性项目,例如生日。我还有其他各种约会 - 但不是今天的约会。

我尝试了不同的日期格式,包括 2013-07-25 00:00:00,但没有运气。我研究了网络,并尝试从 VBA 脚本中复制示例 - 不走运。

感谢其他人的任何想法。

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

【问题讨论】:

  • 你试过这种日期格式Mddyy h:mm tt吗?从这里:msdn.microsoft.com/en-us/library/office/…
  • 感谢您的建议。我试过这个并得到了相当不同的结果。使用 IncludeRecurrences=true,我刚刚得到了定期约会——比如人们的生日。将其设置为 false,我只会得到 3 个生日,而没有其他任何东西 - 而这些生日甚至不是这个月......

标签: c# calendar outlook ms-office


【解决方案1】:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook._items.restrict?view=outlook-pia

该文档使用其他日期格式。 我使用下一个过滤器每天检查新的或修改过的约会。

        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>();

【讨论】:

    【解决方案2】:

    我使用了下面的代码,它运行良好。 我可能使用了太多的 'IncludeRecurrences = false' 但它有效;) 我必须这样做,否则它的行为很奇怪(我认为“IncludeRecurrences”比较事物的方式不同)

    只需将日历作为第一个参数,将 pDateToRead 作为您想要的日期。 (例如)

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

    实际方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2021-07-09
      • 2017-02-18
      相关资源
      最近更新 更多