Below is all the code:
Option Compare Database
Sub ReportOutlookBody()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim BodyText As String
Dim strTableBeg As String
Dim strTableBody As String
Dim strTableEnd As String
Dim strFntNormal As String
Dim strTableHeader As String
Dim strFntEnd As String
Dim lngID As Long
Dim strSQL As String
Dim mailaddress As Variant
Dim strSubject As String
Dim OutApp As Object, OutMail As Object, signature As String
'Define format for output
strTableBeg = "<br><br><table border=1 cellpadding=3 cellspacing=0>"
strTableEnd = "</table><BR><BR>"
strTableHeader = "<font size=3 face=" & Chr(34) & "Calibri" & Chr(34) & "><b>" & _
"<tr bgcolor=lightblue>" & _
TD("DCP:") & _
TD("Customer:") & _
TD("Ship-to:") & _
TD("Order Number:") & _
TD("Purchase Order:") & _
TD("Customer:") & _
TD("Product:") & _
TD("Requested Ship Date:") & _
TD("Plant Anticipated Date:") & _
"</tr></b></font>"
' StrContent = StrContent & "Limit Changes: " & Forms!Main.lstorders.Column(1) & Chr(13)
strFntNormal = "<font color=black face=" & Chr(34) & "Calibri" & Chr(34) & " size=2>"
strFntEnd = "</font>"
With Forms!Main!lstorders
For Each varItem In Forms!Main!lstorders.ItemsSelected
strList = strList & Forms!Main!lstorders.Column(0, varItem) & ","
Next
If strList <> "" Then
strList = Left(strList, Len(strList) - 1)
strList = "(" & strList & ")"
strSQL = "SELECT * FROM tblTrucks WHERE [ID] IN " & strList & ";"
'(send e-mail)
Else
MsgBox ("Please select an order from the list.")
End If
End With
Set rst = CurrentDb.OpenRecordset(strSQL)
strTableBody = strTableBeg & strFntNormal & strTableHeader
Do Until rst.EOF
strTableBody = strTableBody & _
"<tr>" & _
TD(rst![DCP]) & _
TD(rst![Customer]) & _
TD(rst![Ship-to]) & _
TD(rst![Sales Doc]) & _
TD(rst![PO#]) & _
TD(rst![City]) & _
TD(rst![Mat Description]) & _
TD(rst![PlGI date]) & _
TD(rst![Plant Anticipated Date]) & _
"<tr>"
BodyText = "<HTML><BODY>Hi Team,<BR><BR>Below are the Late Notifications for today. Please send out an email to the customer through the Late Order Notification Database <BR></BODY></HTML>"
rst.MoveNext
Loop
strTableBody = strTableBody & strFntEnd & strTableEnd
rst.Close
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail ' This creates a blank email and captures the users default signature.
.BodyFormat = olFormatHTML
.Display
End With
signature = OutMail.HTMLBody
With OutMail
.To = Forms!Main!TxtEmailTo
.CC = Forms!Main!txtCCTo
.BCC = Forms!Main!txtBCCTo
.Subject = Forms!Main!txtSubjectHeading
.HTMLBody = strFntNormal & BodyText & strTableBody & signature
.Display 'or use .Send
.ReadReceiptRequested = False
End With
'outlook tidy up
Set OutMail = Nothing
Set OutApp = Nothing
Set rst = Nothing
End Sub
Function TD(strIn As String) As String
TD = "<TD nowrap>" & strIn & "</TD>"
End Function