【问题标题】:How to restart Thunderbird with VBA (close application & re-open)如何使用 VBA 重启 Thunderbird(关闭应用程序并重新打开)
【发布时间】:2014-05-08 01:24:35
【问题描述】:

我的代码可以使用 Thunderbird 发送带有附件的电子邮件,效果很好。但是,有时必须手动重新打开 Thunderbird 才能使该代码正常工作(很少)。

如果出现错误,如何关闭 Thunderbird 应用程序,重新打开并再次运行此代码?

Public Function fSendThunderbird()

Dim strCommand As String
Dim strTo as string, strCC As String
Dim strSubject As String
Dim strBody As String
Dim strFilePath As String

strTo = "myemail@.cie.com"
strCC = "myemail@.cie.com"
strSubject = ThisWorkbook.Name & " " & Format(Range("E3").Value, "mmmm yyyy")
strFilePath = Application.ActiveWorkbook.FullName
strBody = "Hello"

strCommand = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird"
strCommand = strCommand & " -compose " & "to=" & strTo & "," & "cc=" & strCC & "," & _
"subject=" & strSubject & "," & "attachment=" & strFilePath

Call Shell(strCommand, vbNormalFocus)

End Function

【问题讨论】:

    标签: vba thunderbird


    【解决方案1】:

    您可以自己调用函数,因为它似乎应该通过 Shell 打开 Thunderbird。我强烈建议您获取错误编号以及当这不起作用时发生的情况。

    Public Function fSendThunderbird()
        on error goto errsend
    
        Dim strCommand As String
        Dim strTo as string, strCC As String
        Dim strSubject As String
        Dim strBody As String
        Dim strFilePath As String
    
        strTo = "myemail@.cie.com"
        strCC = "myemail@.cie.com"
        strSubject = ThisWorkbook.Name & " " & Format(Range("E3").Value, "mmmm yyyy")
        strFilePath = Application.ActiveWorkbook.FullName
        strBody = "Hello"
    
        strCommand = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird"
        strCommand = strCommand & " -compose " & "to=" & strTo & "," & "cc=" & strCC & "," & _
        "subject=" & strSubject & "," & "attachment=" & strFilePath
    
        Call Shell(strCommand, vbNormalFocus)
    
    End Function
    
    Exit Function 
        errsend:
             'Highly recommend handling errors here. 49999 is just an example
             If err.No = 49999 then
                'fatal error
                Call KillThunderbird
             else
                Call fSendThunderbird()
             end if
    End Function
    
    Sub KillThunderBird
        Dim oServ As Object
        Dim cProc As Variant
        Dim oProc As Object
        Set oServ = GetObject("winmgmts:")
        Set cProc = oServ.ExecQuery("Select * from Win32_Process")
    
        For Each oProc In cProc
    
            'Rename THUNDERBIRD to match what it comes up in as in Task Manager Processes
    
            If oProc.Name = "THUNDERBIRD.EXE" Then
                errReturnCode = oProc.Terminate()
            End If
        Next oProc
    End Sub
    

    【讨论】:

    • 是的,如果 Thunderbird 未打开,调用 Shell 会打开它,但我得到的错误似乎与 Thunderbird 本身有关。基本上我只需要关闭 Thunderbird 的代码。有什么建议吗?抱歉,我没有错误编号(不经常发生)
    • 我已经用 KillThunderbird 子例程更新了我的代码。您可能需要根据发生的情况在代码中移动它。
    • 好的!正是我需要的,我也可以将此示例用于其他内容。谢谢吉米!
    猜你喜欢
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多