Outlook Interop - 阅读日历并检查私人约会

时间:2016-02-03 14:32:50

标签: c# outlook calendar

所以我正在开发一个应用程序,它可以读取一组个人的Outlook日历并为他们构建行程。除了当我刮掉用户的日历时,我没有得到关于他们标记为私有的事件的任何信息,我已经得到了相当多的工作。

让我明确表示,我并没有试图查看用户的日历并阅读他们最黑暗的秘密约会标题,但我仍然希望能够证明此人在此期间不可用。例如在Outlook中,它只是显示为“私人约会” - 这正是我希望能够在我的应用程序中执行的操作。

以下是我用来抓取用户日历的代码:

    private Calendar CreateCalendar(String User_Name, String Initials, DateTime Week)
    {
        string filter = "[Start] >= '"
            + Week.ToString("g")
            + "' AND [End] <= '"
            + Week.AddDays(7).ToString("g") + "'";

        Microsoft.Office.Interop.Outlook.Application App = new Microsoft.Office.Interop.Outlook.Application();
        Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = App.GetNamespace("MAPI");

        //Resolve the person whose calendar We're trying to read.
        Outlook.Recipient rm = App.Session.CreateRecipient(User_Name);
        if (rm.Resolve())
        {
            try
            {
                Outlook.Folder calFolder = App.Session.GetSharedDefaultFolder(rm, Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder;
                Outlook.Items CalItems = calFolder.Items;
                CalItems.IncludeRecurrences = true;
                CalItems.Sort("[Start]", Type.Missing);
                //Search for Items within the pre-defined time range.

                Outlook.Items restrictItems = CalItems.Restrict(filter);
                if (restrictItems.Count == 0)
                {
                    Debug.Print("No Items Found");
                    return new Calendar(User_Name, Initials);
                }
                else
                {
                    //Create our internal representation of their calendar, for use elsewhere.
                    Calendar cal = new Calendar(User_Name, Initials);
                    Outlook.AppointmentItem i = (Outlook.AppointmentItem)restrictItems.GetFirst();

                    do
                    {
                        cal.AddAppointment(i.Subject, i.Start, i.End);
                        i = restrictItems.GetNext();
                    } while (i != null);

                    App = null;
                    mapiNamespace = null;
                    return cal;
                }
            }
            catch (Exception ex)
            {
                App = null;
                mapiNamespace = null;
                throw new AccessViolationException(User_Name + " was resolved but could not access their calendar.  Have they set you up as a delegate?", ex);
            }
        }
        else
            throw new AccessViolationException("The name " + User_Name + " was not resolved, make sure their name in the RM List is spelled correctly. If you can enter their name as a recipient in an outlook mail message and it resolves correctly when you click 'Check Names', then it should work!");
    }

我不确定私有事件是否包含在outlook的数据结构中的同一个“文件夹”中,所以也许我只是没有找到合适的位置?

我一直在谷歌搜索这个因为“私有”是C#中的一个保留字,基本上每个帖子上都有代码和函数声明。希望你们会更有帮助。

谢谢!

0 个答案:

没有答案