Outlook COMException

时间:2012-11-07 14:32:32

标签: c# com outlook outlook-2010 comexception

System.Runtime.InteropServices.COMException ..您的服务器管理员限制了您可以同时打开的项目数量......

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

也许它的appointment.UserProperties.Find(“myInformation”)导致COMException?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

您需要确保在使用Marshal.ReleaseComObject()完成后立即释放Outlook项目,而不是等待垃圾收集器启动。

您还需要避免使用多点符号,以确保不会获得包含中间结果且无法显式引用和释放的隐式变量(由编译器创建)。

答案 2 :(得分:1)

我找到了解决方法。没有必要使用Find因为Restrict来制作我需要的东西。

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