1) rst![billing EMAIL].Type = 2 doesn't make sense. Type = 2 should be set for the mail recipient, not for the field of the recordset.
2) You don't have a Next corresponding to the For lines.
3) The code is difficult to analyze because of the inconsistent indentation.
The part that you mention first should be like this:
Code: Select all
If Not IsNull(rst![billing EMAIL]) Then
arrNames = Split(rst![billing EMAIL], ",")
For i = 0 To UBound(arrNames)
.Recipients.Add arrNames(i)
Next i
End If
Note how If aligns with End If, and how For aligns with Next. This allows you to see at a glance what belongs together.
Here is the complete code:
Code: Select all
Dim strWhere As String
Dim strSQL As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strFilename As String
Dim outApp As Object
Dim outMsg As Object
Dim blnStart As Boolean
Dim arrNames As Variant
Dim i As Long
On Error Resume Next
' Try to get running instance of Outlook
Set outApp = GetObject(Class:="Outlook.Application")
If outApp Is Nothing Then
' If Outlook wasn't running, start it
Set outApp = CreateObject(Class:="Outlook.Application")
If outApp Is Nothing Then
' We failed to start Outlook, so get out
MsgBox "We can't start Outlook, sorry!", vbCritical
Exit Sub
End If
' Set a flag that we started Outlook
blnStart = True
End If
On Error GoTo ErrHandler
' Create the where-condition
strWhere = " WHERE EmailFaxSent = False"
If Not IsNull(Me.txtStart) Then
'strWhere = strWhere & " AND BillingPeriodStartDate >= #" & Format(Me.txtStart, "yyyy-mm-dd") & "#"
End If
If Not IsNull(Me.txtEnd) Then
'strWhere = strWhere & " AND BillingPeriodEndDate <= #" & Format(Me.txtEnd, "yyyy-mm-dd") & "#"
End If
' ***** Change 10 to the number of records to process *****
strSQL = "SELECT ALL * FROM [tblHellWeekautoemail]" & strWhere
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
Do While Not rst.EOF
glngInvoiceID = rst!InvoiceID
strFilename = gstrPath & rst!WalmartNumber & "-" & _
rst!STORE_ID & Format(Me.txtStart, " mmmm yyyy") & " Missing Control Sheets" & ".pdf"
' Export report to PDF
' ConvertReportToPDF "rptInvoiceAuto", , strFilename, , False
DoCmd.OutputTo acOutputReport, "rptHellweekMissingControlSheets", acFormatPDF, strFilename
' Create a new e-mail message
Set outMsg = outApp.CreateItem(0) ' olMailItem
With outMsg
' Use the e-mail address field
'Main Email
arrNames = Split(rst!Email, ",")
For i = 0 To UBound(arrNames)
.Recipients.Add arrNames(i)
Next i
If Not IsNull(rst![billing EMAIL]) Then
arrNames = Split(rst![billing EMAIL], ",")
For i = 0 To UBound(arrNames)
.Recipients.Add arrNames(i)
Next i
End If
If Not IsNull(rst![other 1 EMAIL]) Then
arrNames = Split(rst![other 1 EMAIL], ",")
For i = 0 To UBound(arrNames)
.Recipients.Add(arrNames(i)).Type = 2
Next i
End If
If Not IsNull(rst![owner2EMAIL]) Then
arrNames = Split(rst![ownerEMAIL2], ",")
For i = 0 To UBound(arrNames)
.Recipients.Add(arrNames(i)).Type = 2
Next i
End If
' Change the subject as needed
.Subject = "Test Email"
' Change the body text as needed& vbCrLf & vbCrLf & Me.comment3
.Body = Me.Comment1 & vbCrLf & vbCrLf & Me.comment2 & vbCrLf & vbCrLf & "Sincerely," & vbCrLf & vbCrLf & "Rebecca Turner"
' Attach the PDF file
.Attachments.Add strFilename
.Send ' to edit the message before sending
End With
'Set EmailFaxSent field to True, and EmailDateTimeSent to Now
rst.Edit
rst!EmailFaxSent = True
rst!EmailDateTimeSent = Now
rst.Update
' Optional: delete the PDF file after creating the e-mail
' Delete or comment out the next line if you don't want to delete the file
Kill strFilename
rst.MoveNext
Loop
' Optional: requery the list box
Me.lbxInvoice.Requery
'Updates dates sent in tbl invoice
'DoCmd.OpenQuery "qryUpdateInvoiceAutoEmailDateSent"
'MsgBox "Emails are complete"
ExitHandler:
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
If blnStart Then
outApp.Quit
End If
Set outMsg = Nothing
Set outApp = Nothing
Exit Sub
ErrHandler:
If Err = 2501 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End If