我需要当前的预约。如果当前没有预约,则下一个甚至上一个预约。
我想使用 Restrict
来限制约会集,然后根据限制参数选择第一个或最后一个约会(例如,限制为当前时间之后结束的约会,或当前时间之前开始的约会) 。
我在使用作为参数所需的字符串过滤器时遇到问题。
一个简单的 VB 示例(代码树桩):
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?我找不到任何有关如何操作 [Start] 的文档,例如将其解析为日期或其他内容。
根据日期格式,我要么得到错误的约会,要么由于过滤器排除了所有约会而无法使用 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< /a>,如果使用 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);
}
}
I need the current appointment. If no current appointment then the next or even previous appointment.
I figure to use Restrict
to limit the set of appointments, and then choose either the first or last appointment depending on the restrict argument (e.g. Restrict to appointments ending after current time, or appointments starting before current time).
I'm having trouble with the string filter needed as argument.
A simple VB example (code stump):
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]"
I am attempting to do the same in 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();
I imagine since the filter is a string, the date format needs to be yyyy/mm/dd HH:mm:ss? I can't find any documentation on how to manipulate the [Start], like parsing it to a date or something.
Depending on the date format, I will either get the wrong appointment, or will be unable to use GetLast due to the filter excluding all appointments.
I've seen examples, but either they loop through the appointments (too inefficient), or the date formats look like they can't be trusted to return the correct appointment (For example https://social.msdn.microsoft.com/Forums/vstudio/en-US/c6a8bd21-6534-43be-b23e-1068651da92e/retrieve-appointment-items-from-outlook-2007-calendar-restrict?forum=vsto, which seems to have the date hardcoded instead if using DateTime.Now
.)
UPDATE: I'm currently looping through like shown below. Any suggestions for more efficient code?
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);
}
}
发布评论
评论(4)
这是你的问题:
我认为你想要的是:
This is your issue:
What I think you're going for is:
通过遵循信息发现 在这里,我能够使用“yyyy-MM-dd HH:mm”作为 toString 调用的格式字符串来使其工作。
希望这有帮助。
By following the information found here, I was able to get it to work using "yyyy-MM-dd HH:mm" as the format string for the toString call.
Hope this helps.
此代码用于显示从今天开始的 Outlook 约会:
This code works to show Outlook appointments from today onwards:
我无法弄清楚日期时间格式,但我找到了这篇文章:
https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering
然后我意识到他们有一个称为“DASL查询”的概念,进一步阅读我发现这与使用日期时间进行过滤有关:
https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search -and-filter/filtering-items-using-a-date-time-comparison
我决定以这种方式获取今天的所有 AppointmentItem(s):
我希望尽可能安全,因为似乎过滤by DateTime 与本地 DateTime 设置相关。通过使用“今天”宏,我第一次能够从 Outlook 约会存储中提取正确的信息
I couldn't figure out DateTime format, but I found this article:
https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering
Then I realized that they have a concept called "DASL Queries", reading furthermore I found this related to filtering with DateTime:
https://learn.microsoft.com/en-us/office/vba/outlook/how-to/search-and-filter/filtering-items-using-a-date-time-comparison
And I've decided to get all today's AppointmentItem(s) this way:
I want to be safest as possible because it seems that filtering by DateTime is related to local DateTime settings. By using the "today" macro I was able for the first time to extract the correct information from Outlook appointments store