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

时间:2015-06-03 21:08:33

标签: vba excel-vba email outlook excel

我想要一个宏在完成后通过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("jon.doe@aol.com")
OlMail.Recipients.Add ToRecipient
Next ToRecipient

For Each CcRecipient In Array("jon.doe@aol.com")
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

2 个答案:

答案 0 :(得分: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("jon.doe@aol.com")
        OlMail.Recipients.Add ToRecipient
    Next ToRecipient

    For Each CcRecipient In Array("jon.doe@aol.com")
        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("jon.doe@aol.com","jon.doe@aol.com")

答案 1 :(得分:0)

Sub AUTOGENERATEEMAIL()

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strBody = "<FONT SIZE = 3>Good day all, " & "<br>" & "<br>" & "Please see attached." & "<br>" & "<br>" & "Pleasant Regards"

With OutMail
    .Display
    .To = "Roti@hotmail.com; sall@hotmail.com; mj@hotmail.com; "
    .CC = ""
    .BCC = ""
    .Subject = "Finance" & path
    .HTMLBody = strBody & .HTMLBody
    .Attachments.Add ("\\Finance\Company Shared Folders\UserShares\Finance Tool\Finance Assistant Tools\Finance.xlsb")
    .Display

End With

Set OutMail = Nothing
Set OutApp = Nothing

End Sub