Outlook邮件自动化-仅在Outlook已经运行时错误 [英] Outlook mail automation - error only when Outlook is already running

查看:220
本文介绍了Outlook邮件自动化-仅在Outlook已经运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击WinForm中的发送"按钮时,我写了一个子程序以通过Outlook发送电子邮件.如果关闭Outlook,我的代码可以正常工作.当Outlook已经打开时,我仍然收到OutMail = oApp.CreateItem(0)

I have written a sub to send an e-mail through Outlook when I click a send button located in a WinForm. If Outlook is closed, my code works fine. When Outlook is already opened, I keep getting an error with OutMail = oApp.CreateItem(0)

OfficeAutomation.exe中发生了'System.InvalidCastException'类型的未处理异常

An unhandled exception of type 'System.InvalidCastException' occurred in OfficeAutomation.exe

其他信息:无法将类型为"Microsoft.Office.Interop.Outlook.ApplicationClass"的COM对象转换为接口类型为"Microsoft.Office.Interop.Outlook._Application". 该操作失败是因为在COM组件上对具有IID'{00063001-0000-0000-C000-000000000046}'的接口的QueryInterface调用由于以下错误而失败:RPC服务器不可用. (来自HRESULT的异常:0x800706BA).

Additional information: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).

Private Sub EmailOut_Click(sender As Object, e As EventArgs) Handles EmailOut.Click
    Dim oApp As Outlook.Application = Nothing

    ' Check whether there is an Outlook process running.
    Dim outlookRunning As Integer = Process.GetProcessesByName("OUTLOOK").Length
    If outlookRunning > 0 Then
        ' If Outlook is running, use the GetActiveObject method to obtain the 
        ' process and cast it to an Application object.
        Try
            oApp = TryCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application"),  _
                Outlook.Application)
        Catch generatedExceptionName As System.Exception
            oApp = TryCast(Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")),  _
                Outlook.Application)
        Finally
            ' Kill Outlook and then restart it as a last resort.
            Dim workers As Process() = Process.GetProcessesByName("OUTLOOK")
            For Each worker As Process In workers
                worker.Kill()
                worker.WaitForExit()
                worker.Dispose()
            Next
        End Try
    Else
        ' If Outlook is not running, create a new instance of Outlook.
        oApp = New Outlook.Application()
        Dim ns As Outlook.NameSpace = oApp.GetNamespace("MAPI")
        Try
            ' try to use default profile and do not open a window
            ' if login fails, then we must pop up a window and let the 
            ' user choose a profile to allow access
            ns.Logon("", "", False, System.Reflection.Missing.Value)
        Catch generatedExceptionName As System.Exception
            ' use default profile and pop up a window
            ns.Logon("", "", True, True)
        End Try
        ns = Nothing
    End If
    Dim OutMail As Outlook.MailItem

    OutMail = oApp.CreateItem(0)

    With OutMail
        If Not String.IsNullOrEmpty(EmailAdr.Text) Then
            .To = EmailAdr.Text
        Else
            MessageBox.Show("Please enter an e-mail address", "Missing Recipient", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If Not String.IsNullOrEmpty(CcAdr.Text) Then
            .CC = CcAdr.Text
        End If
        .Subject = OutSubject.Text
        .Body = OutTextBox.Text
        System.Threading.Thread.Sleep(5000)
        .Send()
    End With
    System.Threading.Thread.Sleep(5000)
    oApp = Nothing
    Microsoft.VisualBasic.Shell("taskkill /F /IM outlook.exe", Microsoft.VisualBasic.vbHide)
End Sub

推荐答案

不要使用GetActiveObject-Outlook是单例,CreateInstance将返回指向已在运行的实例的指针.

Do not use GetActiveObject - Outlook is a singleton, CreateInstance will return a pointer to the already running instance.

这篇关于Outlook邮件自动化-仅在Outlook已经运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆