4

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?

MikroDel
  • 6,705
  • 7
  • 39
  • 74
  • thx for upvotes ) thats mean someone read it and find it good ) – MikroDel Nov 20 '12 at 11:43
  • @Anubis1233 - I have posted my answer – MikroDel Apr 02 '13 at 07:22
  • thanks but I was already using restrict :/ – Florian Apr 02 '13 at 09:08
  • @Anubis1233 you have exception with using Restrict? Have you created a question with your problem? – MikroDel Apr 02 '13 at 09:16
  • Yes I am having the exception with Rescrict. Sadly it is a random exception. I "fixed" it by cutting the result after the first 5 results. My Exchange admin has ensured me that this Error should not happen at this low number of entrys. – Florian Apr 02 '13 at 09:22
  • @Anubis1233 can you maybe post it as a question? With source code. Maybe you will become than the better solution that your one. – MikroDel Apr 02 '13 at 09:23

3 Answers3

3

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Great, thank you - I had the same problem, always closed the object but releasing the ComObject was the solution :) – Jan021981 Mar 25 '21 at 14:00
3

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
CareyG
  • 31
  • 2
1

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);
...
MikroDel
  • 6,705
  • 7
  • 39
  • 74