使用来自单元格的Excel信息从Outlook发送自动电子邮件

时间:2017-09-29 14:15:31

标签: excel-vba vba excel

enter image description here

我需要使用Excel Outlook从Excel发送自动电子邮件,我正在尝试编码但无法执行此操作。我已附上表格供您参考。

Sub SendEmail()

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

With OutMail
    .To = "ABC@gmail.com"
    .CC = ""
    .BCC = ""
    .Subject = "Report"
    .Body = "Hello!"
    .Attachments.Add ActiveWorkbook.FullName

    .Send
End With

Set OutMail = Nothing
Set OutApp = Nothing
Application.OnTime TimeValue("17:00:00"), "SendEmail"

End Sub

1 个答案:

答案 0 :(得分:0)

在创建电子邮件之前,您需要引用并登录Outlook.Namespace。

尝试将此添加到您的代码中:

Set OutApp = CreateObject("Outlook.Application")

'** -> add this block here
Dim OutNS as Object
Set OutNS = OutApp.GetNamespace("MAPI")
OutNS.Logon
'**

Set OutMail = OutApp.CreateItem(0)