Outlook COMException

主宰稳场 提交于 2020-01-03 17:36:25

问题


System.Runtime.InteropServices.COMException ..Your server administrator has limited the number of items you can open simultaneously...

at Microsoft.Office.Interop.Outlook._AppointmentItem.get_UserProperties()

        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 ;
                    }
                }
            }
        }

Maybe its appointment.UserProperties.Find("myInformation") cause COMException?


回答1:


When done with an Outlook Mailitem close it then release it

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



回答2:


You need to make sure you release Outlook items as soon as you are done with them using Marshal.ReleaseComObject() instead of waiting for the Garbage Collector to kick in.

You also need to avoid multiple dot notation to make sure you do not get implicit variables (created by the compiler) that hold that intermediary results and cannot be explicitly referenced and released.




回答3:


I have found the solution for this. It isnt necessary to use Find cause Restrict make what I need.

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


来源:https://stackoverflow.com/questions/13271829/outlook-comexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!