2015-03-31 103 views
0

我想從任何給定用戶的名字檢索當天的約會。 到目前爲止,我可以通過下面的代碼檢索我自己的約會。檢索公共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) 返回的錯誤是「‘System.Runtime.InteropServices.COMException’類型的未處理的異常出現在mscorlib.dll

其他信息:從HRESULT異常:0x8834010F「

任何幫助,使這項工作將不勝感激,謝謝。

+0

立即是否異常火災或只有你處理了幾個約會後? – 2015-03-31 14:38:09

+0

只有輸入用戶名並按回車鍵後才輸入 – CheesyMeat 2015-03-31 14:45:17

+0

如果我輸入自己的名字,則返回約會,但如果我輸入其他人,則返回錯誤。可能是權限問題?我有權訪問公共日曆,因此應該可以將約會拉好? – CheesyMeat 2015-03-31 16:00:57

回答

0

我建議從立即發佈底層COM對象開始。完成使用後,請使用System.Runtime.InteropServices.Marshal.ReleaseComObject釋放Outlook對象。如果您的加載項嘗試枚舉存儲在Microsoft Exchange Server上的集合中的超過256個Outlook項目,這一點尤其重要。如果您沒有及時釋放這些物品,您可以達到Exchange對任何時候打開的物品的最大數量施加的限制。然後在Visual Basic中將變量設置爲Nothing(C#中的空值)以釋放對該對象的引用。有關更多信息,請參閱Systematically Releasing Objects

而且我建議使用查找/FindNext中限制類項目的方法。您可以在以下文章瞭解更多關於他們: