2016-09-15 163 views
1

我想要做的就是自動從Thunderbird帳戶發送電子郵件。用戶甚至不必點擊電子郵件的發送按鈕。自動發送電子郵件從雷鳥與Excel VBA

我試過使用CDO,但問題在於您必須輸入您從中發送的帳戶的用戶名和密碼。這個宏將被用於幾個不同的賬戶,所以輸入每個用戶名和密碼是不可行的。如果有人從Thunderbird中檢索用戶名,密碼和smtp服務器,我可以使用CDO,但是我覺得我已經擁有的代碼應該能夠在沒有CDO的情況下實現這一點(希望)。

這是我看到的唯一的代碼(它無處不在),就是爲了完成這個。

Sub Thunderbird() 

Dim thund As String  
Dim email As String 
Dim cc As String 
Dim bcc As String 
Dim subj As String 
Dim body As String  

email = "[email protected]" 
cc = "[email protected]" 
bcc = "[email protected]" 
subj = "Subject" 
body = "body text" 

thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" 
thund = thund & " -compose " & Chr$(34) & "mailto:" & email & "?" 
thund = thund & "cc=" & Chr$(34) & cc & "?" 
thund = thund & "bcc=" & Chr$(34) & bcc & "?" 
thund = thund & "subject=" & Chr$(34) & subj & Chr$(34) 
thund = thund & "body=" & Chr$(34) & body 

Call Shell(thund, vbNormalFocus) 
SendKeys "^+{ENTER}", True 

End Sub 

截至目前,該領域ccbccsubjbody都是正確識別。問題是他們都被添加到第一個字段的末尾。例如,現在代碼的方式,cc將被放入cc字段,但bcc,subjbody都會被附加到Thunderbird的cc字段中的cc

如果我評論cc出來,然後bcc被放在正確的領域,但subjbody獲得雷鳥的密件抄送字段添加到bcc

如果我評論ccbcc出來,然後subj被放置在正確的領域,但body獲取雷鳥的主題字段添加到subj

所以基本上我需要在每一行的末尾添加正確的代碼。我試過"?"Chr$(34)都無濟於事。

最後,SendKeys "^+{ENTER}", True根本不起作用。這可能是因爲沒有將所有參數放在Thunderbird的正確字段中,但不確定,因爲我無法正常工作。來自Thunderbird的電子郵件會顯示,但此代碼並不像發送電子郵件那樣發送。

SOLUTION(由@zedfoxus作爲提供)

Sub Thunderbird() 

Dim thund As String  
Dim email As String 
Dim cc As String 
Dim bcc As String 
Dim subj As String 
Dim body As String  

email = "[email protected]" 
cc = "[email protected]" 
bcc = "[email protected]" 
subj = "Subject" 
body = "body text" 

thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" & _ 
     " -compose " & """" & _ 
     "to='" & email & "'," & _ 
     "cc='" & cc & "'," & _ 
     "bcc='" & bcc & "'," & _ 
     "subject='" & subj & "'," & _ 
     "body='" & body & "'" & """" 

Call Shell(thund, vbNormalFocus) 
Application.Wait (Now + TimeValue("0:00:03")) 
SendKeys "^{ENTER}", True 

End Sub 
+0

[通過Thunderbird發送Excel宏](https:// stackoverflow。com/questions/31283948/excel-macro-sending-thunderbird/31284639#31284639) – 0m3r

回答

3

你是八九不離十。試試這個:

Public Sub SendEmail() 
    Dim thund As String 
    Dim email As String 
    Dim cc As String 
    Dim bcc As String 
    Dim subj As String 
    Dim body As String 

    email = "[email protected]" 
    cc = "[email protected]" 
    bcc = "[email protected]" 
    subj = "Testing" 
    body = "Testing" 

    thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe " & _ 
      "-compose " & """" & _ 
      "to='" & email & "'," & _ 
      "cc='" & cc & "'," & _ 
      "bcc='" & bcc & "'," & _ 
      "subject='" & subj & "'," & _ 
      "body='" & body & "'" & """" 

    Call Shell(thund, vbNormalFocus) 
    SendKeys "^+{ENTER}", True 

End Sub 

注意從http://kb.mozillazine.org/Command_line_arguments_(Thunderbird)的例子。

thunderbird -compose「to ='john @ example.com,kathy @ example.com',cc ='britney @ example.com',subject ='dinner',body ='今晚的晚餐怎麼樣? 」,附件= 'C:\ TEMP \ info.doc,C:\ TEMP \ food.doc'」

的例子表明,-compose後,我們應該用我們的輸入雙引號的信息。每個參數用逗號分隔。命名是parameter='value[,value]' [,parameter='value[,value]]...

+0

謝謝!不幸的是,SendKeys位仍然不起作用。電子郵件顯示正確,但SendKeys不會發送電子郵件。它實際上只是最小化窗口。 – user2951249

+0

你有什麼版本的Windows?你有什麼版本的Excel?我有Office 365 ProPlus和SendKeys與Windows 7上的Thunderbird完美配合。 – zedfoxus

+0

試試'Application.SendKeys「%fl」,True'而不是'SendKeys「^ + {ENTER}」,True'。如果這不起作用,請嘗試'SendKeys「^ +〜」,True' – zedfoxus