检索公用Outlook日历约会



我正试图从任何给定的用户名中检索当天的约会。到目前为止,我可以用下面的代码检索我自己今天的约会。

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();

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

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)返回的错误为"mscorlib.dll 中发生类型为"System.Runtime.InteropServices.COMException"的未处理异常

附加信息:HRESULT异常:0x8834010F"

如有任何帮助,我们将不胜感激,谢谢。

我建议从立即释放底层COM对象开始。使用System.Runtime.InteropServices.Marshal.ReleaseComObject可以在使用完Outlook对象后释放该对象。如果外接程序试图枚举存储在Microsoft Exchange Server上的集合中的256个以上的Outlook项目,则这一点尤为重要。如果您不及时释放这些对象,您可能会达到Exchange对任何时候打开的最大项目数的限制。然后在Visual Basic中将变量设置为Nothing(在C#中为null)以释放对对象的引用。有关详细信息,请参见系统地释放对象。

此外,我建议使用Items类的Find//strong>FindNext或Restrict方法。你可以在以下文章中阅读更多关于它们的信息:

  • 如何:使用Find和FindNext方法检索Outlook日历项目
  • 如何:在Outlook中使用Restrict方法获取日历项目

最新更新