2

Short question: how to close outlook items properly after using them?


Code to reproduce the problem:

Dim olApp As New Microsoft.Office.Interop.Outlook.Application
Dim olSelection As Microsoft.Office.Interop.Outlook.Selection = olApp.ActiveExplorer.Selection

For i As Integer = 1 To olSelection.Count   'Outlook starts counting at 1
    Dim olItem As Object = olSelection(i)
    Dim sSubject As String = olItem.Subject
    olItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard)
    Runtime.InteropServices.Marshal.ReleaseComObject(olItem)
Next

Explanation:
It is possible to copy outlook items (MailItem, DocumentItem, PostItem, basically any item) into my application. To achieve this, I iterate over the selected items of the active outlook window. It works fine, but when more than 250 (it might be a different number depending on configuration) items are selected, a COMExeption is thrown:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.VisualBasic.dll

Additional information: Your server administrator has limited the number of items you can open simultaneously. Try closing messages you have opened or removing attachments and images from unsent messages you are composing.

I tried to close the items when I don't need them any more, but it does not seem like it does anything.


Other questions about the same error
I know about this other question that is about the same error, but I already follow the advise of the first two answers and the third, accepted (and last) answer doesn't fit into my context

Community
  • 1
  • 1
Breeze
  • 2,010
  • 2
  • 32
  • 43
  • Have you checked [this answer](http://stackoverflow.com/questions/36826927/outlook-add-in-crashes-or-your-server-administrator-has-limited-the-number-of-it?rq=1) ? In short, after `ReleaseComObject()`, set the reference the olItem to `Nothing`. – Martin Verjans Apr 28 '16 at 07:51
  • @SuperPeanut Thank you for the reply, but adding `olItem = Nothing` after `ReleaseComObject` didn't help – Breeze Apr 28 '16 at 07:55

2 Answers2

1

Thanks to @Dmitry Streblechenko, who pointed out that the Selection collection holds references to the items, I found a solution. It removes the items from Selection after processing them and renews the collection at every exception.

Here is the code:

Dim olApp As New Microsoft.Office.Interop.Outlook.Application
Dim olExplorer As Microsoft.Office.Interop.Outlook.Explorer = olApp.ActiveExplorer
Dim olSelection As Microsoft.Office.Interop.Outlook.Selection = olExplorer.Selection

Dim items as New List(Of Object)

While True
    Try
        For i As Integer = 1 To olSelection.Count
            Dim olItem As Object = olSelection(i)
            Dim sSubject As String = olItem.Subject
            olItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard)
            olExplorer.RemoveFromSelection(olItem)
            Runtime.InteropServices.Marshal.ReleaseComObject(olItem)
        Next
        Exit While
    Catch ex As Exception
        Runtime.InteropServices.Marshal.ReleaseComObject(olSelection)
        olSelection = olExplorer.Selection
    End Try
End While
Breeze
  • 2,010
  • 2
  • 32
  • 43
0

There is nothing you can do - the Selection collection itself holds references to the items. Try to turn the cached mode on.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • thank you for your answer, it led me to a solution. I would be happy if you could look at it and tell me if there is something that I overlooked – Breeze May 02 '16 at 06:52