我正在写一个程序,检查预约的前景,基本上我只需要当前的任命 - 如果当前没有预约,然后就在旁边,甚至以前的预约。检索当前Outlook约会
我想这样做我需要使用[restrict method] [1]来限制约会集合,然后根据restrict参数选择第一个或最后一个约会(例如,仅限于约会结束在当前时间之后,或者仅在当前时间之前开始约会)。 我在作为参数需要的字符串过滤器方面遇到了很多麻烦。
一个简单的例子VB(代码残端)如下([源] [2])
myStart = Format(Date, "mm/dd/yyyy hh:mm AMPM")
strRestriction = "[Start] <= '" & myStart & "'"
'Restrict the Items collection
Set oResItems = oItems.Restrict(strRestriction)
'Sort
oResItems.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 AREA
String 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 appointment
restrictedItems.Sort("[Start]");
Outlook.AppointmentItem oAppt = restrictedItems.GetLast();
// Done. Log off.
oNS.Logoff();
首先,我想,因为所述过滤器是一个字符串,日期格式必须是YYYY/MM/DD hh:mm:ss的?我似乎无法找到如何操作[开始],喜欢它解析为一个日期或东西的任何文档(我尝试了各种各样的事情,但没有任何工程)。根据我使用的日期格式,我将在得到错误的约会,否则程序将无法使用GetLast由于不包括所有约会的过滤器。我已经看到几个人声称他们已经开始工作的例子,但他们要么循环约会(效率太低),要么日期格式看起来像他们不能被信任返回正确的任命(好像是这个家伙social.msdn.microsoft.com/Forums/en-US/vsto/thread/c6a8bd21-6534-43be-b23e-1068651da92e,谁也似乎如果使用已经硬编码的日期,而不是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);
}
}
非常感谢!一切似乎都像现在一样工作。似乎我忘记了M的月份:S – Madsn 2011-02-19 23:54:16