Email Template Editor
After having initial email code you need to create the email body which in most cases is HTML. For this VBA Mailer provides the Email Template Editor. This program is also available from the VBA Mailer menu.
THe Email Template Editor has most commonly used HTML editing features. It allows you to write the email like with a normal word processor, which allows you to focus on the message instead of struggling with HTML syntax. If you right-click on the canvas a menu opens allowing Voice Typing. Inspect opens a sophisticated HTML code editor, allowing you to use any HTML feature you require.
On closing the Email Template Editor creates the VBA HTML code ('HTML Template') that is used to fill the MailItem's HTMLBody property. Converting HTML to VBA is bidirectional. If you need to alter the email body content (or Subject, To, etcetera for that matter) you can start the editor again to make the changes there. After saving it using 'Return', the VBA code is updated.
Variable pane
On the right side of the Template Editor is a variable pane.
From here you can drag variables present in the email macro such as strName into the HTML code.
From the pane you can also have New Variables added to the template, which are automatically include in the email macro on Save.
Email Template Editor VBA code generation
Manually writing HTML for your email body results in code lines cluttered with HTML start and end tags. A simple example (the full code of the current example can be seen here):
Dim strName As String
strName = "Pete"
mi.HTMLBody = "<p>Hello " & strName & ",</p>" & _
"<p>How are you today? Please check out <a href=""https://https://www.codevba.com/vba-mailer/"">VBA Mailer</a>!</p>"
When you edit the HTMLBody using VBA Mailer Template Creator, lines are inserted that use HTML Element functions to encapsulate the HTML tag details and double quotes in attributes.
HTML Element node expressions
In the code below you see the use of an array n of String. The nodes n(i) expressions reflect the nesting structure of the email body content.
Dim strName As String
strName = "Pete"
Dim n(4) As String
n(2) = p("Hello " & strName & ",")
n(4) = a("VBA Mailer", href:="https://https://www.codevba.com/vba-mailer/")
n(3) = p("How are you today? Please check out " & n(4) & "!")
n(1) = n(2) & n(3)
.HTMLBody = n(1)
The content of the template designer image above would become
Set mi = appMailer.CreateItem(OlItemType.olMailItem)
Dim strFirstName As String: strFirstName = ControlValue(frm.Controls("First Name"))
Dim strEmailAddress As String: strEmailAddress = ControlValue(frm.Controls("E-mail Address"))
With mi
.To = strEmailAddress
.Subject = "Invitation for " & strFirstName & " to celebrate completion of " & ProjectName
Dim n(27) As String
n(2) = p("Hi " & strFirstName & ",")
n(3) = p("We've been working together on our project " & ProjectName & " for nearly a year, but it has been worth it!")
n(5) = li("Great design,")
n(6) = li("Much attention to details in implementation and testing.")
n(4) = ol(n(5) & n(6), "style=list-style-type: decimal;")
n(10) = th("Phase")
n(11) = th("Actual")
n(12) = th("Planned")
n(9) = tr(n(10) & n(11) & n(12))
n(8) = thead(n(9))
n(15) = td("Design")
n(16) = td("February 15")
n(17) = td("February 26")
n(14) = tr(n(15) & n(16) & n(17))
n(19) = td("Version 1")
n(20) = td("July 3")
n(21) = td("June 25")
n(18) = tr(n(19) & n(20) & n(21))
n(23) = td("Version 2")
n(24) = td("November 29")
n(25) = td("December 3")
n(22) = tr(n(23) & n(24) & n(25))
n(13) = tbody(n(14) & n(18) & n(22))
n(7) = table(n(8) & n(13), "style=border-color: black;")
n(26) = br()
n(27) = p("To celebrate we will have a dinner with music on Friday, I hope to see you all then!")
n(1) = n(2) & n(3) & n(4) & n(7) & n(26) & n(27)
.HTMLBody = n(1)
.Send
End With