D Schlachter
D Schlachter

Reputation: 221

Outlook Addin: Avoid open items limit

My Outlook addin (VB.net) iterates through mail items and computes their size, like so:

For Each m in fol.Items
     b = b + m.Size
Next

This works well, unless the Exchange folder has more than about 250 items in it, in which case I get an exception Your server administrator has limited the number of items you can open simultaneously. I understand from this that my code doesn't release each mail item as it iterates, so I'm ending up with hundreds of 'open' files in Outlook.

I attempted to add a m.Close() line to the loop, but I see that this isn't the desired behavior.

How can I release the objects as I process them?

Upvotes: 1

Views: 1458

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66255

Firstly, Marshal.ReleaseComObject is a good idea. Secondly, "for each" loop in .Net tends to keep all collection items referenced until the loop exits - use a "for" loop instead.

And most importantly, there is absolutely no reason to open all items in a folder - use the Table object (MAPIFolder.GetTable). See http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.table(v=office.14).aspx

Upvotes: 1

D Schlachter
D Schlachter

Reputation: 221

I changed the code to

For Each m in fol.Items
    b = b + m.Size
    Marshal.ReleaseComObject(m)
Next

It seems inefficient, but it works.

Upvotes: 0

Related Questions