如何使用VBA重启Thunderbird(关闭应用程序并重新打开)

时间:2014-03-28 14:12:48

标签: vba thunderbird

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

如果我有错误,如何关闭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

1 个答案:

答案 0 :(得分: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
相关问题