打开太多 outlook 文件时出现 COMException
COMException when too many outlook files were opened
小问题:使用后如何正确关闭outlook项目?
重现问题的代码:
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
解释:
可以将 outlook 项目(MailItem
、DocumentItem
、PostItem
,基本上任何项目)复制到我的应用程序中。为此,我遍历了活动 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.
当我不再需要它们时,我尝试关闭它们,但它似乎没有任何作用。
关于同一错误的其他问题
我知道 this 另一个关于相同错误的问题,但我已经听从了前两个答案的建议,而第三个被接受的(也是最后一个)答案不符合我的上下文
您无能为力 - Selection 集合本身包含对项目的引用。尝试打开缓存模式。
感谢@Dmitry Streblechenko,他指出 Selection
集合包含对项目的引用,我找到了解决方案。它在处理后从 Selection
中删除项目,并在每次异常时更新集合。
代码如下:
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
小问题:使用后如何正确关闭outlook项目?
重现问题的代码:
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
解释:
可以将 outlook 项目(MailItem
、DocumentItem
、PostItem
,基本上任何项目)复制到我的应用程序中。为此,我遍历了活动 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.
当我不再需要它们时,我尝试关闭它们,但它似乎没有任何作用。
关于同一错误的其他问题
我知道 this 另一个关于相同错误的问题,但我已经听从了前两个答案的建议,而第三个被接受的(也是最后一个)答案不符合我的上下文
您无能为力 - Selection 集合本身包含对项目的引用。尝试打开缓存模式。
感谢@Dmitry Streblechenko,他指出 Selection
集合包含对项目的引用,我找到了解决方案。它在处理后从 Selection
中删除项目,并在每次异常时更新集合。
代码如下:
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