2012-11-07 36 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