我有一个电子邮件地址列表,但我只需要向相应单元格中“过期”的个人发送电子邮件。单击按钮时,它会打开多个 Outlook 窗口。
这是我到目前为止所拥有的:
Private Sub CommandButton1_Click()
Dim c As Range
For Each c In Range("F5:F42")
If c.Value2 = "Expired" Then Call Mail_small_Text_Outlook(c.Offset(0, -3).Value2)
Next c
End Sub
Sub Mail_small_Text_Outlook(emailAddress As String)
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set ws = Sheets("Sheet1")
Set Rng = ws.Range(ws.Range("A1"), ws.Range("A" & Rows.Count).End(xlUp))
For Each cell In Rng
If cell.Value2 = "Expired" Then
SendTo = SendTo & cell.Value & ";"
End If
Next
strbody = "Hello," & vbNewLine & vbNewLine & _
"You are receiving this email because your Wastewater Pathogens (Annual) is now expired or will expire within the next 30 days. Please sign up for the next available class in LMS. If you are unable to sign up in LMS, please contact Christa Scott." & vbNewLine & _
"Thank you and have a nice day."
On Error Resume Next
With OutMail
.To = SendTo
.CC = ""
.BCC = ""
.Subject = "Expired Hazmat Right-to-Know Training - " & Date
.Body = strbody
.Display 'or .Send to automatically send email.
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub