通过VBA发送带有附件的Outlook电子邮件

问题描述:

我希望宏在完成后通过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

确保您参考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