如果我将c.Offset(, 1)
更改为c.Offset(, 0)
,则会将电子邮件发送给第一个收件人,但不会发送到下一个。如果我将c.Offset(, 0)
更改为c.Offset(, 1)
,我得到的Outlook无法识别一个或多个名称。如何获得正确的语法以将电子邮件发送给多个用户?电子表格的设计如下以及VB
。我为冗长的信息道歉,只是想完成。谢谢 :)。VBA以电子邮件的方式发送多个地址
电子表格
A B C D
Email Date Comment 1 Comment 2
[email protected]
[email protected]
的设计时,电子表格打开下面的自动运行:
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
由于您使用的是'For Each c In Rng',因此您希望每行发送一封电子邮件。但从你的问题来看,这听起来像你想发送同一封电子邮件到多个电子邮件地址。你能澄清你想要做的吗? –
'c.Offset(,1)'是抵消一个*列*,而不是一个*行*这可能是你想要的。 –