2015-06-03 360 views
1

我希望宏在完成後通過Outlook通過Outlook向電子郵件發送電子郵件。通過VBA發送帶有附件的Outlook電子郵件

我正在測試這與我自己和同事的電子郵件地址,我得到一個「無法送達」Error

該消息表示無法聯繫到收件人,並建議稍後嘗試發送電子郵件。

如果社區會查看我迄今爲止產生的代碼,並讓我知道它是我的代碼還是導致錯誤的系統,我將不勝感激。 (我有一個強烈的感覺,它是代碼!)

Sub CreateEmail() 

Dim OlApp As Object 
Dim OlMail As Object 
Dim ToRecipient As Variant 
Dim CcRecipient As Variant 

Set OlApp = CreateObject("Outlook.Application") 
Set OlMail = OlApp.createitem(olmailitem) 

For Each ToRecipient In Array("[email protected]") 
OlMail.Recipients.Add ToRecipient 
Next ToRecipient 

For Each CcRecipient In Array("[email protected]") 
With OlMail.Recipients.Add(CcRecipient) 
.Type = olCC 
End With 

Next CcRecipient 

'Fill in Subject field 
OlMail.Subject = "Open Payable Receivable" 


'Add the report as an attachment 

OlMail.Attachments.Add ("C:\OpenPayRecPrint2.pdf") 


'Send Message 

OlMail.Send 


End Sub 
+0

Excel的VBA沒有按」不知道「olCC」的價值。如果您使Outlook應用程序可見並註釋掉髮送行,則可以更輕鬆地找出發生了什麼問題。 –

+0

@Tim - 如果OP已設置對Outlook對象庫的引用,則Excel VBA將知道olCc的值。 – ChipsLetten

+0

@Tim Williams謝謝你的見解,我使用CC部分的撇號,我能夠發送電子郵件。有什麼方法可以在我熟悉的當前代碼中設置一個CC列表?我需要在「Set ..」區域建立olCC嗎? – Michael

回答

2

確保您參考Outlook對象庫

Option Explicit 
Sub CreateEmail() 

    Dim OlApp As Object 
    Dim OlMail As Object 
    Dim ToRecipient As Variant 
    Dim CcRecipient As Variant 

    Set OlApp = CreateObject("Outlook.Application") 
    Set OlMail = OlApp.createitem(olmailitem) 

    For Each ToRecipient In Array("[email protected]") 
     OlMail.Recipients.Add ToRecipient 
    Next ToRecipient 

    For Each CcRecipient In Array("[email protected]") 
     With OlMail.Recipients.Add(CcRecipient) 
     .Type = olcc 
    End With 
    Next CcRecipient 

    'Fill in Subject field 
    OlMail.Subject = "Open Payable Receivable" 


    'Add the report as an attachment 
    OlMail.Attachments.Add ("C:\temp\test1.xlsx.") 
    OlMail.Display ' <--for testing, to send use OlMail.Send 

    'OlMail.Send 
End Sub 

添加多個CcRecipient In Array("[email protected]","[email protected]")

+0

感謝您的洞察! – Michael

+0

@Michael不錯的照片個人資料lol – 0m3r

+0

哈哈謝謝你,我覺得你是第一個注意到....「ONE LOOK?!?!」 – Michael

相關問題