gpt4 book ai didi

c# - 检索当前的 Outlook 约会

转载 作者:行者123 更新时间:2023-11-30 15:09:02 26 4
gpt4 key购买 nike

我需要当前约会。如果当前没有约会,则下一个甚至上一个约会。

我打算使用 Restrict 来限制约会集,然后根据 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?我找不到任何关于如何操作 [开始] 的文档,例如将其解析为日期或其他内容。

根据日期格式,我会得到错误的约会,或者由于过滤器排除了所有约会而无法使用 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);
}
}

最佳答案

这是你的问题:

DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM")

我认为您的目的是:

DateTime.Now.ToString("MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture)

关于c# - 检索当前的 Outlook 约会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4914680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com