VBA通过电子邮件发送多个地址

时间:2015-10-23 15:47:59

标签: excel-vba vba excel

如果我将c.Offset(, 1)更改为c.Offset(, 0),则会将电子邮件发送给第一个收件人,但不会发送给下一个收件人。如果我将c.Offset(, 0)更改为c.Offset(, 1),我认为Outlook无法识别一个或多个名称。如何才能正确地将语法发送给多个用户?电子表格的设计如下,VB。我为冗长的信息道歉,只是想完成。谢谢你:)。

设计电子表格

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

当电子表格打开时,下面会自动运行:

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 个答案:

答案 0 :(得分:2)

您需要的只是.To = c,因为您发送的内容已发送到包含地址的A列。

根本不需要偏移范围内的c单元格。

如果您希望向多个地址发送电子邮件,则需要在每个地址之间使用分号,因为这是Outlook解决的方式,即存在多个地址。

所以,基于上面的例子:

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

请注意,我也将c偏移1 。你写了c.Offset(,1)意味着它会偏移1列。 Offset的论据是Offset(rows,columns,[row height],[column width])