2015-10-23 113 views
1

如果我將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 
+2

由於您使用的是'For Each c In Rng',因此您希望每行發送一封電子郵件。但從你的問題來看,這聽起來像你想發送同一封電子郵件到多個電子郵件地址。你能澄清你想要做的嗎? –

+0

'c.Offset(,1)'是抵消一個*列*,而不是一個*行*這可能是你想要的。 –

回答

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])

+1

或者,在For循環中,OP可以執行'.Recipients.Add c.Offset(_iterator _)。Value' –

+1

Thanks @DavidZemens!如果有很多行要添加,那肯定會更好。 –

+0

我設置了'.To = c',現在發送多個郵件的問題是第一封電子郵件是正確的。我會在一個新的線程中發佈。謝謝 :)。 – Chris

相關問題