使用VBA向多个收件人发送电子邮件

问题描述:

我有以下代码,它允许我附加报告,然后将其发送给一个收件人。使用VBA向多个收件人发送电子邮件

如何将它发送到多个地址?

我试着把地址放在一个数组中,但它给出了“类型不匹配”错误。

Dim strReportName As String 
Dim oLook As Object 
Dim oMail As Object 
Dim olns As Outlook.Namespace 
Dim strTO As String 
Dim strCC As String 
Dim strMessageBody As String 
Dim strSubject As String 

Set oLook = CreateObject("Outlook.Application") 
'Set olns = oLook.GetNamespace("MAPI") 
Set oMail = oLook.CreateItem(0) 

'*********************** USER DEFINED SECTION ************************ 
strTO = "[email protected]" 
strMessageBody = "<---This is an automatically generated email. Please do not respond.---->" 
strSubject = "Daily Skip" 
'********************************************************************* 

With oMail 
.To = strTO 
.CC = strCC 
.Body = strMessageBody 
.Subject = strSubject 

.Attachments.Add "C:\Output Reports\SkipLotReport.xlsx" 
.Send 
End With 

Set oMail = Nothing 
Set oLook = Nothing 
'Set olns = Nothing 


'DB.Close 
'tbloutput.Close 
'dbLocal.Close 
objWorkbook.Close 

'Set objmail = Nothing 
'Set DB = Nothing 
Set tbloutput = Nothing 


Set objWorksheet = Nothing 
Set objWorkbook = Nothing 
Set objExcel = Nothing 
Set tbloutput = Nothing 
Set dbLocal = Nothing 

分号分隔的电子邮件地址:

strTO = "[email protected];[email protected];[email protected]" 

由于@HansUp说在一个评论,如果您在数组中有你的电子邮件地址已经,您可以使用Join功能将其转换以分号分隔的字符串:

strTO = Join(YourArrayVariable, ";") 

strTO是一个字符串。

使用与在“收件人”框中手动输入相同的格式。

strTO =“[email protected]; [email protected]