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

如何使用VBA重新启动Thunderbird(关闭应用程序并重新打开),vba,thunderbird,Vba,Thunderbird,我有使用Thunderbird发送带有附件的电子邮件的代码,效果很好。然而,有时必须手动重新打开Thunderbird才能使代码正常工作(很少) 如果出现错误,如何关闭Thunderbird应用程序,重新打开并再次运行此代码 Public Function fSendThunderbird() Dim strCommand As String Dim strTo as string, strCC As String Dim strSubject As String Dim strBody As

我有使用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

您可以调用函数本身,因为它似乎应该通过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本身有关。基本上我只需要关闭雷鸟的代码。有什么建议吗?对不起,我没有错误号(不经常发生),我已经用KillThunderbird子例程更新了代码。您可能需要根据正在发生的事情在代码中移动它。好的!这正是我所需要的,我也可以用这个例子来做其他的事情。谢谢你,吉米!