使用 VBA 向多个收件人发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25524955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending emails to multiple recipients using VBA
提问by Chrislaar123
I have the following code which allows me to attach a report then send it to one recipient.
我有以下代码,它允许我附加报告,然后将其发送给一个收件人。
How do I send it to more than one address?
如何将其发送到多个地址?
I've tried putting the addresses in an array but it gives a "Type Mismatch" error.
我试过将地址放在一个数组中,但它给出了“类型不匹配”错误。
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
回答by Jean-Fran?ois Corbett
Semicolon-separated e-mail addresses:
以分号分隔的电子邮件地址:
strTO = "[email protected];[email protected];[email protected]"
As @HansUp remarked in a comment, if you have your email addresses already in an array, you can use the Join
function to convert it to a semicolon-delimited string:
正如@HansUp 在评论中所说,如果您的电子邮件地址已经在一个数组中,您可以使用该Join
函数将其转换为以分号分隔的字符串:
strTO = Join(YourArrayVariable, ";")
回答by niton
strTO is a string.
strTO 是一个字符串。
Use the same format as you would enter manually in the To box.
使用与您在“收件人”框中手动输入的格式相同的格式。
strTO = "[email protected]; [email protected]"
strTO = "[email protected]; [email protected]"