david_johnson
Registered User.
- Local time
- Today, 16:54
- Joined
- Oct 12, 2017
- Messages
- 12
So I don't know if this is my limited experience with HTML or what but it seems what I need to do is simple, though it won't work. I need to create an email with some variables here and there, and then underneath I need an HTML table that pulls from an Access table. The part I thought difficult, the table, is already complete and works very well. I cannot figure out how to add text before the table, the body of the email. I would be most grateful is someone could show me how to add some lines of HTML text and have them included.
Private Sub Command1_Click()
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 4) As String
Dim aRow(1 To 4) As String
Dim aBody() As String
Dim lCnt As Long
Dim bBody As String
'Create the header row
aHead(1) = "Contact Role"
aHead(2) = "Contact Name"
aHead(3) = "Contact Phone Number"
aHead(4) = "Contact Email"
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 test_table"
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("contact_type")
aRow(2) = rec("contact_name")
aRow(3) = rec("contact_phone")
aRow(4) = rec("contact_email")
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 = "example@example.com"
olItem.Subject = "Test E-mail"
olItem.htmlbody = Join(aBody, vbNewLine)
olItem.display
End Sub
Private Sub Command1_Click()
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 4) As String
Dim aRow(1 To 4) As String
Dim aBody() As String
Dim lCnt As Long
Dim bBody As String
'Create the header row
aHead(1) = "Contact Role"
aHead(2) = "Contact Name"
aHead(3) = "Contact Phone Number"
aHead(4) = "Contact Email"
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 test_table"
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("contact_type")
aRow(2) = rec("contact_name")
aRow(3) = rec("contact_phone")
aRow(4) = rec("contact_email")
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 = "example@example.com"
olItem.Subject = "Test E-mail"
olItem.htmlbody = Join(aBody, vbNewLine)
olItem.display
End Sub