Hi guys. I'm trying to loop through a table to send several emails at once with a delayed delivery for each email. The code works great up until I get past for first loop. When I run the code, it displays the first email just like it should, but none of the other emails come up. The weird thing is that when I debug it with F8 the varaibles all change appropriately and I get the new information just fine, the email just doesnt display.
Also, the code doesn't send an email at all when I change it from .display to .send, but that's another, less important point.
Any help is appreciated. Thanks in advance.
Also, the code doesn't send an email at all when I change it from .display to .send, but that's another, less important point.
Any help is appreciated. Thanks in advance.
Code:
Private Sub CommandButton1_Click()
'Monthly
Dim MonthlyDate As Date 'Date to Send
Dim MonthlyTime As Double 'Time to Send
Dim FullName As String 'Employee
Dim FirstName As String
Dim Main As Worksheet
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String 'body of the email
Set OutApp = CreateObject("Outlook.Application") 'establishes outlook as a partner application
Set OutMail = OutApp.CreateItem(0) 'creates outlook window
Set Main = ThisWorkbook.Sheets("Sheet1") 'variable for the order requests worksheet
For y = 3 To 8
For x = 3 To 7
MonthlyDate = Cells(x, 1)
MonthlyTime = Cells(x, 2)
TimeConverted = CDate(MonthlyTime)
TimeAndDate = MonthlyDate + TimeConverted
Item = Cells(2, y)
FullName = Cells(x, y)
FirstName = Right(FullName, Len(FullName) - Application.WorksheetFunction.Find(" ", FullName))
strbody = "Hi " & FirstName & "," & vbNewLine & vbNewLine & _
"text here"
On Error Resume Next
With OutMail
Test = Application.WorksheetFunction.VLookup(FullName, ThisWorkbook.Sheets("Lists").Range("tblEmployees"), 2, False)
.To = Application.WorksheetFunction.VLookup(FullName, ThisWorkbook.Sheets("Lists").Range("tblEmployees"), 2, False)
.CC = ""
.BCC = ""
.Subject = ""
.Body = strbody
.DeferredDeliveryTime = TimeAndDate
.Display 'Change to Send after debugging
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Next x
Next y
End Sub