none
Outlook COMException RRS feed

  • Question

  • 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?

    Wednesday, November 21, 2012 8:32 AM

All replies

  • most probably you are opening too many items and not releasing them in timely manner. Every object from OOM that you access should be released at the end of its usage by calling Marshal.ReleaseComObject, so inside your loop if you are done with appointment, userproperties and single user property object, call that function on each of them. Same for Items and folder object outsiude of loop.
    Wednesday, November 21, 2012 8:57 AM
  • Wednesday, November 21, 2012 9:04 AM
  • In each loop pass you are creating a new appointment item and never releasing it. That is creating a new object in each pass. You are also creating an invisible UserProperties collection in each pass that's not being released.
     
    Move these declarations outside the loop, and set each to null at the end of each loop pass:
     
    var appointment = null;
    Outlook.UserProperties props = null;    // set this to appointment.UserProperties in each pass
     
    Also, no matter what that article says, in many cases you will need to use Marshal.ReleaseComObject() as well as GC.Collect() and WaitForPendingFinalizers(), then GC.Collect() again after a number of loop passes.
     
    You will have to determine empirically if that's necessary. I often find that after a 100 or so passes I need that cleanup code so the RPC channels allocated for each object are released and the memory errors don't happen.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "MikroDel" <=?utf-8?B?TWlrcm9EZWw=?=> wrote in message news:665cf7fc-5e48-4417-84cf-3b49ddf907df...

    in CLR its not always necessary

    When to use ReleaseComObject vs FinalReleaseComObject?


    Ken Slovak MVP - Outlook
    • Marked as answer by Quist ZhangModerator Wednesday, November 28, 2012 2:47 AM
    • Unmarked as answer by MikroDel Monday, December 3, 2012 12:16 PM
    Wednesday, November 21, 2012 2:46 PM
    Moderator
  • Ken  and what is about this Article from Paul Harrington? ( The "silent assassin" section)

    Marshal.ReleaseComObject Considered Dangerous

    Ill try to user Filter with Items.Ristrict


    • Edited by MikroDel Wednesday, November 21, 2012 7:55 PM
    Wednesday, November 21, 2012 7:54 PM
  • For one thing, all Office dll's and components are unmanaged code, not managed code.
     
    Second, when you use a shared addin with the COM Shim wizard or VSTO you get a unique AppDomain for your code that isolates any of your RCW's from any others used by other code. So no release in other code will affect you, as it would with no AppDomain isolation.
     
    Finally, as usual it all depeds on the circumstances and the code used. The RCW is not released unless there is a 0 refcount on the object. If you declare and instantiate the object in your code and release it when done release of the RCW doesn't matter as you no longer need those objects. You declare others when needed, which creates a new RCW.
     
    We've been using this sort of thing for many years now, and when done correctly you do not get RCW errors and you don't get those RPC channel memory exceptions.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "MikroDel" <=?utf-8?B?TWlrcm9EZWw=?=> wrote in message news:594ccabb-e530-434e-89c1-f5baaa6cca0f...

    Ken  and what is about this Article from Paul Harrington? ( The "silent assassin" section)

    Marshal.ReleaseComObject Considered Dangerous

    Ill try to user Filter with Items.Ristrict



    Ken Slovak MVP - Outlook
    • Marked as answer by Quist ZhangModerator Wednesday, November 28, 2012 2:47 AM
    • Unmarked as answer by MikroDel Monday, December 3, 2012 12:17 PM
    Wednesday, November 21, 2012 8:32 PM
    Moderator