检索公共Outlook日历约会?

时间:2015-03-31 14:29:25

标签: c# outlook calendar

我正在尝试从任何给定用户的名称中检索同一天的约会。 到目前为止,我可以使用下面的代码检索我今天的约会。

else if (UserSelection == "2")
            {
                //Create the Outlook application
                Outlook.Application oApplication = new Outlook.Application();

                // Get the NameSpace and Logon information.
                Outlook.NameSpace oNameSpace = oApplication.GetNamespace("mapi");

                //Log on by using a dialog box to choose the profile.
                oNameSpace.Logon(Missing.Value, Missing.Value, true, true);

                // Get the Calendar folder.
                Outlook.MAPIFolder oCalendar = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

                // Get the appointments (items) collection from the Calendar folder.
                Outlook.Items oItems = oCalendar.Items;
                oItems.IncludeRecurrences = true;


                List<Outlook.AppointmentItem> lst = new List<Outlook.AppointmentItem>();

                foreach (Outlook.AppointmentItem item in oItems)
                {
                    if (item.Start.Day == DateTime.Today.Day && item.Start.Month == DateTime.Today.Month && item.Start.Year == DateTime.Today.Year)
                    {
                        Console.WriteLine("Organizer: " + item.Organizer);
                        Console.WriteLine("Start: " + item.Start.ToString());
                        Console.WriteLine("End: " + item.End.ToString());
                        Console.WriteLine("Location: " + item.Location);
                        Console.WriteLine("Recurring: " + item.IsRecurring);
                        Console.WriteLine("Subject: " + item.Subject);
                        Console.WriteLine("Attendees: " + item.OptionalAttendees);
                        Console.WriteLine("");
                    }
                }


                //Get the last appointment(item)
                //Outlook.AppointmentItem oAppt = (Outlook.AppointmentItem)oItems.GetLast();

                //Show the appointment(item) in outlook.
                //oAppt.Display(true);

                // Done. Log off.
                oNameSpace.Logoff();

                //Clean up.
                oItems = null;
                oCalendar = null;
                oNameSpace = null;
                oApplication = null;

                Console.ReadLine();

这很好用。但是,下面的代码不会返回给定用户&#34; userName&#34;的约会。

if (UserSelection == "1")
        //try (used for error handling) 
        {
            string userName = Console.ReadLine();


            Outlook.Application oApplication;
            oApplication = new Outlook.Application();
            Outlook.NameSpace oNameSpace = oApplication.GetNamespace("mapi");
            oNameSpace.Logon(Missing.Value, Missing.Value, true, true);

            Outlook.Recipient oRecip = (Outlook.Recipient)oNameSpace.CreateRecipient(userName);
            Outlook.MAPIFolder oCalendar = (Outlook.MAPIFolder)oNameSpace.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar);

            // Get the appointments (items) collection from the Calendar folder.
            Outlook.Items oItems = oCalendar.Items;
            oItems.IncludeRecurrences = true;



            List<Outlook.AppointmentItem> lst = new List<Outlook.AppointmentItem>();

            foreach (Outlook.AppointmentItem item in oItems)
            {
                if (item.Start.Day == DateTime.Today.Day && item.Start.Month == DateTime.Today.Month && item.Start.Year == DateTime.Today.Year)
                {
                    Console.WriteLine("Organizer: " + item.Organizer);
                    Console.WriteLine("Start: " + item.Start.ToString());
                    Console.WriteLine("End: " + item.End.ToString());
                    Console.WriteLine("Location: " + item.Location);
                    Console.WriteLine("Recurring: " + item.IsRecurring);
                    Console.WriteLine("Subject: " + item.Subject);
                    Console.WriteLine("Attendees: " + item.OptionalAttendees);
                    Console.WriteLine("");
                }
            }



                //Show the appointment(item) in outlook.
                //oAppt.Display(true);

                // Done. Log off.
                oNameSpace.Logoff();

                // Clean up.

                oItems = null;
                oCalendar = null;
                oNameSpace = null;
                oApplication = null;

                Console.ReadLine();

产生错误的代码行是foreach (Outlook.AppointmentItem item in oItems) 返回的错误是&#34;未处理的类型&#39; System.Runtime.InteropServices.COMException&#39;发生在mscorlib.dll

附加信息:来自HRESULT的异常:0x8834010F&#34;

非常感谢任何帮助完成这项工作,谢谢。

1 个答案:

答案 0 :(得分:0)

我建议立即从发布底层COM对象开始。完成使用后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。如果您的加载项尝试枚举存储在Microsoft Exchange Server上的集合中超过256个Outlook项目,这一点尤为重要。如果您未及时发布这些对象,则可以达到Exchange对任何时候打开的最大项目数的限制。然后在Visual Basic中将变量设置为Nothing(C#中为null)以释放对该对象的引用。有关详细信息,请参阅Systematically Releasing Objects

另外,我建议使用Items类的查找 / FindNext 限制方法。您可以在以下文章中阅读有关它们的更多信息: