After searching I finally came up with a script to put table information in the body of an outlook email. I have a few questions to enhance this.
1, The last field aRow(8) needs to have a percent format.
2, I would like at somepoint to be able to add another table to the same email. I suume this may be a bit difficult.
Anyway this does work pretty well as it stands. Open to improvements though.
I also thought this script may help someone else who struggled with the code that works.
Thanks
1, The last field aRow(8) needs to have a percent format.
2, I would like at somepoint to be able to add another table to the same email. I suume this may be a bit difficult.
Anyway this does work pretty well as it stands. Open to improvements though.
I also thought this script may help someone else who struggled with the code that works.
Thanks
Code:
Option Compare Database
Private Sub btnEmail_Click()
'On Error GoTo Errorhandler
Dim olApp As Object
Dim olItem As Variant
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim strQry As String
Dim aHead(1 To 8) As String
Dim aRow(1 To 8) As String
Dim aBody() As String
Dim lCnt As Long
'Create the header row
aHead(1) = "ProduOrd"
aHead(2) = "Desc"
aHead(3) = "PersoName"
aHead(4) = "HoursPosted"
aHead(5) = "OpTextDescr"
aHead(6) = "Plan_Hrs"
aHead(7) = "Booked"
aHead(8) = "Perf"
lCnt = 1
ReDim aBody(1 To lCnt)
aBody(lCnt) = "<HTML><body><table border='2'><tr><th>" & Join(aHead, "</th><th>") & "</th></tr>"
'Create each body row
strQry = "SELECT * From tblTimePostPrevDay"
Set db = CurrentDb
Set rec = CurrentDb.OpenRecordset(strQry)
If Not (rec.BOF And rec.EOF) Then
Do While Not rec.EOF
lCnt = lCnt + 1
ReDim Preserve aBody(1 To lCnt)
aRow(1) = rec("ProdOrd")
aRow(2) = rec("Description")
aRow(3) = rec("PersName")
aRow(4) = rec("HrsPosted")
aRow(5) = rec("OpTextDesc")
aRow(6) = rec("PlanHrs")
aRow(7) = rec("booked")
aRow(8) = rec(Format("perf", "percent"))
aBody(lCnt) = "<tr><td>" & Join(aRow, "</td><td>") & "</td></tr>"
rec.MoveNext
Loop
End If
aBody(lCnt) = aBody(lCnt) & "</table></body></html>"
'create the email
Set olApp = CreateObject("Outlook.application")
Set olItem = olApp.CreateItem(0)
olItem.display
olItem.To = "[EMAIL="productionmanager@yahoo.com"]productionmanager@yahoo.com[/EMAIL]"
olItem.Subject = "Production Status Report"
olItem.htmlbody = Join(aBody, vbNewLine)
olItem.display
End Sub