我需要当前的约会.如果没有当前约会,则下一个甚至之前的约会.
我想使用限制来限制约会集,然后根据限制参数选择第一个约会或最后一个约会(例如,限制到在当前时间之后结束的约会或在当前时间之前开始的约会).
我遇到了需要作为参数的字符串过滤器的麻烦.
一个简单的VB示例(代码树桩):
myStart = Format(Date, "mm/dd/yyyy hh:mm AMPM") strRestriction = "[Start] <= '" & myStart & "'"'Restrict the Items collectionSet oResItems = oItems.Restrict(strRestriction)'SortoResItems.Sort "[Start]"
我正在尝试在C#中执行相同的操作.
// Create the Outlook application.Outlook.Application oApp = new Outlook.Application();// Get the NameSpace and Logon information.// Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi");Outlook.NameSpace oNS = oApp.GetNamespace("mapi");//Log on by using a dialog box to choose the profile.oNS.Logon(Missing.Value, Missing.Value, true, true);// Get the Calendar folder.Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);// Get the Items (Appointments) collection from the Calendar folder.oItems = oCalendar.Items;oItems.IncludeRecurrences = true;// THIS IS THE PROBLEM AREAString filter = "[Start] <= '" DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM") "'";Outlook.Items restrictedItems = oItems.Restrict(filter);// Take the last item on the list - should be current or next appointmentrestrictedItems.Sort("[Start]");Outlook.AppointmentItem oAppt = restrictedItems.GetLast();// Done. Log off.oNS.Logoff();
我想因为过滤器是一个字符串,所以日期格式需要为yyyy / mm / dd HH:mm:ss?我找不到任何有关如何操作[开始]的文档,例如将其解析为日期或其他内容.
根据日期格式,我将获得错误的约会,或者由于筛选器将所有约会排除在外而无法使用GetLast.
我看过一些示例,但是它们要么遍历约会(效率低下),要么看起来像日期格式,似乎无法信任它们返回正确的约会(例如https://social.msdn.microsoft.com/Forums/vstudio/en-US/c6a8bd21-6534-43be-b23e-1068651da92e/retrieve-appointment-items-from-outlook-2007-calendar-restrict?forum=vsto,如果使用,则似乎将日期硬编码了DateTime.Now.)
更新:我目前正在遍历如下所示.对于更有效的代码有什么建议吗?
DateTime currentTime = DateTime.Now;foreach (Outlook.AppointmentItem item in oItems){ if (item.Start <= currentTime && item.End.Subtract(new TimeSpan(0, 10, 0)) > currentTime) { appointmentArrayList.Add(item); }}
解决方法:
通过遵循找到的here信息,我能够使用“ yyyy-MM-dd HH:mm”作为toString调用的格式字符串来使其工作.
希望这可以帮助.
来源:https://www.icode9.com/content-1-514901.html联系客服