创建Outlook对象时,部署系统上抛出了未指定的错误

时间:2016-08-30 11:33:36

标签: vb.net outlook office-automation

我有一个与Outlook集成的应用程序,只是为了发送带附件的电子邮件(VB.NET .net v4)。

因为我需要它与多个版本的Outlook一起运行,所以我使用后期绑定。

在我的本地系统和另一个部署系统上,一切正常(Outlook 2016)。

在3个特定系统上,我无法创建" Outlook.Application"对象,因为抛出异常,不幸的是,错误告诉我什么。如果Outlook正在运行,这恰好就会发生。 (所有系统都运行赢10)

我正在从网络共享运行应用程序,但是我已经尝试从c:drive和documents目录本地运行。也尝试以管理员身份运行。

我尝试过的另一件事是检查Outlook是否正在运行以获取对象,而不是创建,但是如果Outlook正在运行则错误仍然存​​在。

这就是我正在使用的:

Public Class Email

    Public Shared Sub EnviaEmail(ByVal pEnderecoDestino() As String, ByVal pAssunto As String, ByVal pBody As String, Optional ByVal pEnderecoCC() As String = Nothing, Optional ByVal pEnderecoBCC() As String = Nothing, Optional ByVal pAnexos() As String = Nothing)

        Dim objOutlook As Object
        Dim objMensagem As Object
        Dim objEnderecos As Object
        Dim objEndereco As Object
        Dim objAnexos As Object

        Try
            Dim fechaApp As Boolean = False

            If Process.GetProcessesByName("OUTLOOK") Is Nothing Then fechaApp = True

            objOutlook = CreateObject("Outlook.Application")          

            objMensagem = objOutlook.CreateItem(0)

            With objMensagem
                .Subject = pAssunto
                .BodyFormat = 1
                .Body = pBody
            End With

            objEnderecos = objMensagem.Recipients

            For i As Integer = 0 To pEnderecoDestino.Length - 1 Step 1
                If Not String.IsNullOrEmpty(pEnderecoDestino(i)) Then
                    objEndereco = objEnderecos.Add(pEnderecoDestino(i).Trim())
                    objEndereco.Type = 1
                    objEndereco.Resolve()
                End If
            Next i

            If Not pEnderecoBCC Is Nothing Then
                For i As Integer = 0 To pEnderecoBCC.Length - 1 Step 1
                    If Not String.IsNullOrEmpty(pEnderecoBCC(i)) Then
                        objEndereco = objEnderecos.Add(pEnderecoBCC(i).Trim())
                        objEndereco.Type = 3
                        objEndereco.Resolve()
                    End If
                Next i
            End If

            If pEnderecoCC IsNot Nothing Then
                For i As Integer = 0 To pEnderecoCC.Length - 1 Step 1
                    If Not String.IsNullOrEmpty(pEnderecoCC(i)) Then
                        objEndereco = objEnderecos.Add(pEnderecoCC(i).Trim())
                        objEndereco.Type = 2
                        objEndereco.Resolve()
                    End If
                Next i
            End If

            objAnexos = objMensagem.Attachments

            If pAnexos IsNot Nothing Then
                For i As Integer = 0 To pAnexos.Length - 1 Step 1
                    objAnexos.Add(pAnexos(i))
                Next i

            End If


            objMensagem.Send()


            If fechaApp Then objOutlook.Quit()

        Catch ex As COMException

            Select Case ex.ErrorCode

                Case -2147467260
                    Throw New Exception("Permission denied to access MS Outlook", ex.InnerException)

                Case -2147221164
                    Throw New Exception("MS Outlook not installed" , ex.InnerException)

                Case Else
                    Throw ex
            End Select

        Finally
            objAnexos = Nothing
            objEndereco = Nothing
            objEnderecos = Nothing
            objMensagem = Nothing
            objOutlook = Nothing
        End Try
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

如果Outlook已经运行,您只看到错误的事实很可能意味着Outlook和您的应用程序在不同的安全上下文中运行。 Outlook是单例,因此它尝试连接到已在运行的实例。但是,COM系统不会在具有不同安全上下文的进程之间编组调用。