检索当前 Outlook 约会

发布于 2024-10-16 03:26:11 字数 2444 浏览 4 评论 0 原文

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

我想使用 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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

超可爱的懒熊 2024-10-23 03:26:11

这是你的问题:

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

我认为你想要的是:

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

This is your issue:

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

What I think you're going for is:

DateTime.Now.ToString("MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture)
我一直都在从未离去 2024-10-23 03:26:11

通过遵循信息发现 在这里,我能够使用“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.

倾城月光淡如水﹏ 2024-10-23 03:26:11

此代码用于显示从今天开始的 Outlook 约会:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DemoAppointmentsInRange();
    }

    private void DemoAppointmentsInRange()
    {
        Application a = new Application();
        Microsoft.Office.Interop.Outlook.Folder calFolder = a.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar) as Microsoft.Office.Interop.Outlook.Folder;
        DateTime start = DateTime.Now;
        DateTime end = start.AddDays(5);
        Microsoft.Office.Interop.Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
        if (rangeAppts != null)
        {
            foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts)
            {
               Response.Write("Subject: " + appt.Subject + " "+" Start: "+appt.Start.ToString()+" "+"End:"+appt.End.ToString()+"<br/>");
            }
        }
    }
    private Microsoft.Office.Interop.Outlook.Items GetAppointmentsInRange(
    Microsoft.Office.Interop.Outlook.Folder folder, DateTime startTime, DateTime endTime)
    {
        string filter = "[Start] >= '"+ startTime.ToString("g")+ "' AND [End] <= '"  + endTime.ToString("g") + "'";
        //Response.Write(filter);
        try
        {
            Microsoft.Office.Interop.Outlook.Items calItems = folder.Items;
            calItems.IncludeRecurrences = true;
            calItems.Sort("[Start]", Type.Missing);
            Microsoft.Office.Interop.Outlook.Items restrictItems = calItems.Restrict(filter);
            if (restrictItems.Count > 0)
            {
                return restrictItems;
            }
            else
            {
                return null;
            }
        }
        catch
        {
            return null;
        }
    }
}

This code works to show Outlook appointments from today onwards:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DemoAppointmentsInRange();
    }

    private void DemoAppointmentsInRange()
    {
        Application a = new Application();
        Microsoft.Office.Interop.Outlook.Folder calFolder = a.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar) as Microsoft.Office.Interop.Outlook.Folder;
        DateTime start = DateTime.Now;
        DateTime end = start.AddDays(5);
        Microsoft.Office.Interop.Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end);
        if (rangeAppts != null)
        {
            foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts)
            {
               Response.Write("Subject: " + appt.Subject + " "+" Start: "+appt.Start.ToString()+" "+"End:"+appt.End.ToString()+"<br/>");
            }
        }
    }
    private Microsoft.Office.Interop.Outlook.Items GetAppointmentsInRange(
    Microsoft.Office.Interop.Outlook.Folder folder, DateTime startTime, DateTime endTime)
    {
        string filter = "[Start] >= '"+ startTime.ToString("g")+ "' AND [End] <= '"  + endTime.ToString("g") + "'";
        //Response.Write(filter);
        try
        {
            Microsoft.Office.Interop.Outlook.Items calItems = folder.Items;
            calItems.IncludeRecurrences = true;
            calItems.Sort("[Start]", Type.Missing);
            Microsoft.Office.Interop.Outlook.Items restrictItems = calItems.Restrict(filter);
            if (restrictItems.Count > 0)
            {
                return restrictItems;
            }
            else
            {
                return null;
            }
        }
        catch
        {
            return null;
        }
    }
}
柠檬色的秋千 2024-10-23 03:26:11

我无法弄清楚日期时间格式,但我找到了这篇文章:

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):

  public enum MacroName
    {
      today,
      tomorrow,
      yesterday,
      next7days,
      last7days,
      nextweek,
      thisweek,
      lastweek,
      nextmonth,
      thismonth,
      lastmonth
    }

    private Outlook.Items GetAppointmentsWithMacro(Outlook.Folder folder, MacroName macro)
    {

      string strFilter = "@SQL=%" + macro.ToString() + "(\"urn:schemas:calendar:dtstart\")%";

      try
      {
        Outlook.Items calItems = folder.Items;
        calItems.IncludeRecurrences = true;
        calItems.Sort("[Start]", Type.Missing);
        Outlook.Items restrictItems = calItems.Restrict(strFilter);
        if (restrictItems.Count > 0)
        {
          return restrictItems;
        }
        else
        {
          return null;
        }
      }
      catch { return null; }
    }

我希望尽可能安全,因为似乎过滤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:

  public enum MacroName
    {
      today,
      tomorrow,
      yesterday,
      next7days,
      last7days,
      nextweek,
      thisweek,
      lastweek,
      nextmonth,
      thismonth,
      lastmonth
    }

    private Outlook.Items GetAppointmentsWithMacro(Outlook.Folder folder, MacroName macro)
    {

      string strFilter = "@SQL=%" + macro.ToString() + "(\"urn:schemas:calendar:dtstart\")%";

      try
      {
        Outlook.Items calItems = folder.Items;
        calItems.IncludeRecurrences = true;
        calItems.Sort("[Start]", Type.Missing);
        Outlook.Items restrictItems = calItems.Restrict(strFilter);
        if (restrictItems.Count > 0)
        {
          return restrictItems;
        }
        else
        {
          return null;
        }
      }
      catch { return null; }
    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文