如果我將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)'是抵消一個*列*,而不是一個*行*這可能是你想要的。 –