如何使用Outlook向Excel VBA中的多个收件人发送电子邮件

use*_*180 9 email excel outlook vba

我正在尝试在Excel表单上设置几个按钮,以通过电子邮件发送给不同的人群.我在单独的工作表上创建了几个单元格范围,以列出单独的电子邮件地址.例如,我想要"按钮A"打开Outlook并从"工作表B:单元格D3-D6"中放入电子邮件地址列表.然后,所有必须完成的操作都在Outlook中点击"发送".

这是我到目前为止的VBA代码,但我无法让它工作.有人可以告诉我我错过了什么或做错了吗?

VB:

Sub Mail_workbook_Outlook_1() 
     'Working in 2000-2010
     'This example send the last saved version of the Activeworkbook
    Dim OutApp As Object 
    Dim OutMail As Object 

    EmailTo = Worksheets("Selections").Range("D3:D6") 

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

    On Error Resume Next 
    With OutMail 
        .To = EmailTo 
        .CC = "person1@email.com;person2@email.com" 
        .BCC = "" 
        .Subject = "RMA #" & Worksheets("RMA").Range("E1") 
        .Body = "Attached to this email is RMA #" & Worksheets("RMA").Range("E1") & ". Please follow the instructions for your department included in this form." 
        .Attachments.Add ActiveWorkbook.FullName 
         'You can add other files also like this
         '.Attachments.Add ("C:\test.txt")

        .Display 
    End With 
    On Error Goto 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 
End Sub 
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 14

您必须循环遍历范围中的每个单元格"D3:D6"并构造您的To字符串.简单地将其分配给变体将无法解决目的.EmailTo如果直接为其指定范围,则变为数组.你也可以这样做,但是你必须循环遍历数组来创建你的To字符串

这是你在尝试什么?(经过试验和测试)

Option Explicit

Sub Mail_workbook_Outlook_1()
     'Working in 2000-2010
     'This example send the last saved version of the Activeworkbook
    Dim OutApp As Object
    Dim OutMail As Object
    Dim emailRng As Range, cl As Range
    Dim sTo As String

    Set emailRng = Worksheets("Selections").Range("D3:D6")

    For Each cl In emailRng 
        sTo = sTo & ";" & cl.Value
    Next

    sTo = Mid(sTo, 2)

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

    On Error Resume Next
    With OutMail
        .To = sTo
        .CC = "person1@email.com;person2@email.com"
        .BCC = ""
        .Subject = "RMA #" & Worksheets("RMA").Range("E1")
        .Body = "Attached to this email is RMA #" & _
        Worksheets("RMA").Range("E1") & _
        ". Please follow the instructions for your department included in this form."
        .Attachments.Add ActiveWorkbook.FullName
         'You can add other files also like this
         '.Attachments.Add ("C:\test.txt")

        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
Run Code Online (Sandbox Code Playgroud)

  • 绝对没有理由建立一个";" 分隔到字符串 - 只需为每个收件人调用MailItem.Recipients.Add. (4认同)
  • 在Google上搜索Latebinding与Earlybinding;) (3认同)

小智 5

ToAddress = "test@test.com"
ToAddress1 = "test1@test.com"
ToAddress2 = "test@test.com"
MessageSubject = "It works!."
Set ol = CreateObject("Outlook.Application")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.RecipIents.Add(ToAddress)
newMail.RecipIents.Add(ToAddress1)
newMail.RecipIents.Add(ToAddress2)
newMail.Send
Run Code Online (Sandbox Code Playgroud)