我使用的脚本将只发送一封电子邮件,而不是200多封电子邮件。
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim LastRow As Integer
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
LastRow = Worksheets("DRIVERS").Cells(Rows.Count, 1).End(xlUp).Row
On Error GoTo cleanup
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
For r = 2 To LastRow
With OutMail
'.To = Worksheets("DRIVERS").Range("G" & r).Value
If Worksheets("DRIVERS").Range("F" & r).Value = "X" Then
.To = "me@me.me" ← Changed for obvious reasons <3
Else
.To = Worksheets("DRIVERS").Range("AL" & r).Value
End If
.Subject = "EDN Roster"
.HTMLBody = "Dear " & Worksheets("DRIVERS").Range("D" & r).Value & "<br /><br />" & _
"Please find your Roster below!<br /><br /><b>This Week:</b><br />" & _
"<table border=1><tr><th></th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th></tr>" & _
"<tr><td>Shift</td><td>" & Worksheets("DRIVERS").Range("H" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("I" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("J" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("K" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("L" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("M" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("N" & r).Value & "</td>" & _
"<tr><td>Sign On</td><td>" & Worksheets("DRIVERS").Range("W" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("Y" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AA" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AC" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AE" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AG" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AI" & r).Value & "</td>" & _
"<tr><td>Sign Off</td><td>" & Worksheets("DRIVERS").Range("X" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("Z" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AB" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AD" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AF" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AH" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AJ" & r).Value & "</td>" & _
"</table><br /><br />" & _
"<b>Next Week:</b><br />" & _
"<table border=1><tr><th></th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th></tr>" & _
"<tr><td>Shift</td><td>" & Worksheets("DRIVERS").Range("O" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("P" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("Q" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("R" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("S" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("T" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("U" & r).Value & "</td>" & _
"<tr><td>Sign On</td><td>" & Worksheets("DRIVERS").Range("AN" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AP" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AR" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AT" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AV" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AX" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AZ" & r).Value & "</td>" & _
"<tr><td>Sign Off</td><td>" & Worksheets("DRIVERS").Range("AO" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AQ" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AS" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AU" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AW" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("AY" & r).Value & "</td><td>" & Worksheets("DRIVERS").Range("BA" & r).Value & "</td>" & _
"</table><br /><br />" & _
"This email is an automated notification, which is unable to receive replies."
.Send 'Display
End With
Next
On Error GoTo 0
Set OutMail = Nothing
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
消息工作,如果我使用.显示它循环通过所有员工的,并更改所有必要的信息.
我看到2016年的一个线程,CreateObject必须在For循环之外,这并没有改变它。
2条答案
按热度按时间sf6xfgos1#
对于那些沿着来说,
Set OutMail = OutApp.CreateItem(0)
必须在for循环中设置!eh57zj3b2#
我知道你已经解决了你的问题,但我建议使用一个单独的潜艇来发送电子邮件和收集信息。我在网上找到了一个,并适应了我的需要:
那么在你的例子中,你的另一个循环只需要把HTMLEbody组装成一个字符串变量,那么
这使得代码更易于阅读和维护,并且您可以在不同的宏中重用email sub。