【问题标题】:COMException when too many outlook files were opened打开太多 Outlook 文件时出现 COMException
【发布时间】:2016-08-22 19:19:10
【问题描述】:

小问题: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 项目(MailItemDocumentItemPostItem,基本上是任何项目)复制到我的应用程序中。为此,我遍历了活动 Outlook 窗口的选定项目。 It works fine, but when more than 250 (it might be a different number depending on configuration) items are selected, a COMExeption is thrown:

Microsoft.VisualBasic.dll 中出现“System.Runtime.InteropServices.COMException”类型的未处理异常

附加信息:您的服务器管理员限制了您可以同时打开的项目数量。尝试关闭您已打开的邮件或从您正在撰写的未发送邮件中删除附件和图像。

当我不再需要它们时,我试图关闭它们,但它似乎没有任何作用。


关于同一错误的其他问题
我知道this 其他关于相同错误的问题,但我已经遵循前两个答案的建议,第三个接受(也是最后一个)答案不适合我的上下文

【问题讨论】:

  • 你检查过this answer 吗?总之,在ReleaseComObject()之后,将olItem的引用设置为Nothing
  • @SuperPeanut 感谢您的回复,但在ReleaseComObject 之后添加olItem = Nothing 并没有帮助

标签: .net vb.net outlook outlook-2010 comexception


【解决方案1】:

感谢@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

【讨论】:

    【解决方案2】:

    您无能为力 - Selection 集合本身包含对项目的引用。尝试开启缓存模式。

    【讨论】:

    • 感谢您的回答,它使我找到了解决方案。如果你能看一下并告诉我是否有我忽略的东西,我会很高兴
    猜你喜欢
    • 2019-03-10
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多