VBA to email multiple address's

2019-08-06 22:41发布

If I change c.Offset(, 1) to c.Offset(, 0) an email will get sent to the first recipient but not the next. If I change c.Offset(, 0) to c.Offset(, 1) I get outlook does not recognize one or more names. How can I get the syntax correct to send the email to multiple users? The design of the spreadsheet is below as well as the VB. I apologize for the lengthy message, just trying to be complete. Thank you :).

Design of spreadsheet

A              B             C                     D
Email         Date         Comment 1             Comment 2
123@gmail.com
456@hotmail.com

when the spreadsheet opens the below runs automatically:

VB

Private Sub Workbook_Open()
Dim sR As String
Dim sFile As String
Sheets("Email").Activate
Range("A1").Select
If MsgBox("Are there any issues to report", vbYesNoCancel) = vbYes Then
        Range("D2").Value = "x"
        MsgBox ("Please select an issue and save"), vbExclamation
Else
Range("C2").Value = "x"
If vbCancel Then Application.SendKeys "%{F11}", True


'define path
 MyFileCopy = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form Attachment.xlsx"

'create connection, check condition, send email
  Set OutApp = CreateObject("Outlook.Application")
  Set WS = ThisWorkbook.Sheets("Email")
With WS
 Set Rng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
End With

For Each c In Rng

 Msg = "For " & WS.Cells(2, 2) & Chr(14) & Chr(14)
 For i = 3 To 4
 If LCase(WS.Cells(c.Row, i)) = "x" Then
    Msg = Msg & "   -" & WS.Cells(1, i) & Chr(14)
 End If
Next

    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = c.Offset(, 1)
        .CC = ""
        .BCC = ""
        .Subject = "Daily Operational Safety Briefing"
        .Body = Msg
        If Range("D2").Value & Chr(14) = "x" Then .Attachments.Add MyFileCopy, 1
        .Send
    End With
Next c

'confirm message sent, clear sheet, and delete copy
 MsgBox "The data has been emailed sucessfully.", vbInformation
 Range("C2:D2").ClearContents
 Kill MyFileCopy

 Set OutMail = Nothing
 Set OutApp = Nothing

'Exit and do not save
 Application.Quit
 ThisWorkbook.Close SaveChanges:=False

 End If
 End Sub

1条回答
Explosion°爆炸
2楼-- · 2019-08-06 23:04

All you need is .To = c because your sent is sent to column A, which has the addresses.

There is no need to offset the c cell in the range at all.

If you wish to send an email to more than one address, semi-colons need to be between each address, as this is how Outlook resolves that there is more than one address.

So, based on your example above:

.To = c & ";" & c.Offset(1) ' & ";" c.Offset(2) to carry it further.

Note that I also Offset c by 1 Row. You wrote c.Offset(,1) meaning it will offset 1 column. The arguments for Offset are Offset(rows,columns,[row height],[column width])

查看更多
登录 后发表回答