2012-11-07 34 views
4

System.Runtime.InteropServices.COMException ..Your服務器管理員已限制在Microsoft.Office.Interop.Outlook._AppointmentItem.get_UserProperties()展望收到COMException

你可以同時打開的項目數...

 var calendar = outlookApplication.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderCalendar); 

     if (calendar == null || calendar.Items == null) 
     { 
      return null; 
     } 

     var calendarItems = calendar.Items; 

     if (calendarItems != null && calendarItems.Count > 0) 
     { 
      // Dont convert to LINQ or foreach please -> may cause Outlook memory leaks. 
      for (int counter = 1; counter <= calendarItems.Count; counter++) 
      { 
       var appointment = calendarItems[counter] as AppointmentItem; 

       if (appointment != null) 
       { 
        var userProperty = appointment.UserProperties.Find("myInformation"); 

        if (userProperty != null && userProperty.Value == myValue) 
        { 
         return appointment ; 
        } 
       } 
      } 
     } 

也許它appointment.UserProperties.Find( 「myInformation」)收到COMException原因?

+0

對upvotes THX)那意味着有人讀它,發現它好) – MikroDel

+0

你找到任何解決方案? – Florian

+0

@ Anubis1233 - 我已經發布了我的回答 – MikroDel

回答

1

我找到了解決方案。沒有必要使用Find原因Restrict使我所需要的。

... 
string filter = string.Format("[myInformation] = '{0}'", myInformation); 
var calendarItems = calendar.Items.Restrict(filter); 
... 
2

你需要確保你儘快釋放Outlook項目,你與他們使用對Marshal.ReleaseComObject()來完成,而不是等待垃圾收集器在踢。

您還需要避免多點符號以確保您不會得到隱式變量(由編譯器創建),該變量包含中間結果並且不能被明確引用和釋放。

3

當與Outlook中的MailItem完成關閉它,然後釋放它

For Each item As Outlook.MailItem In oFolder.Items 
    '<process item code> 

    'Close the item 
    'SaveMode Values 
    'olDiscard = 1 
    'olPromptForSave = 2 
    'olSave = 0 
    item.Close(1) 

    'Release item 
    Marshal.ReleaseComObject(item) 
Next