通过Excel VBA发送带有附件的电子邮件 [英] Sending Email With Attachment Through Excel VBA

查看:601
本文介绍了通过Excel VBA发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Outlook从Excel通过电子邮件发送报告.

I would like to email a report through Outlook from Excel.

我正在用自己和同事的电子邮件地址对此进行测试.我收到无法交付"的 Error .

I am testing this with my own and coworkers' email address. I get an "Undeliverable" Error.

该消息表明无法联系到收件人,并建议稍后尝试发送电子邮件.

The message says the recipient cannot be reached and suggests trying to send the email later.

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

推荐答案

请确保您引用了Outlook对象库

Make sure you reference the Outlook object library

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")

to add multiple CcRecipient In Array("jon.doe@aol.com","jon.doe@aol.com")

这篇关于通过Excel VBA发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆