Emailing HTML

Chrisopia

Registered User.
Local time
Today, 00:14
Joined
Jul 18, 2008
Messages
279
I found this useful tutorial:
http://support.microsoft.com/?id=318881

it's great for sending emails through a database to multiple address with attachments and even importance, however it will only send text which isn't good for marketing purposes or for invoices with the company header on, so I would like to know how to send a HTML version of the email.

I know some people cant download HTML emails, but I will just use "alt" codes for images, it'll be fine!

I've tried just using HTML code but only links work and all coding is displayed
 
Worked a treat!

Code:
Option Compare Database
Option Explicit
Sub SendMessages(Optional AttachmentPath)
Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim ObjOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim TheAddress As String
Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("tblMailingList")
MyRS.MoveFirst
'Create the Outlook Session.
Set objOutlook = CreateObject("Outlook.Application")
Do Until MyRS.EOF
'Create the e-mail message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
TheAddress = MyRS![EmailAddress]
With objOutlookMsg
'Add the To recipients to the e-mail message.
Set ObjOutlookRecip = .Recipients.Add(TheAddress)
ObjOutlookRecip.Type = olTo
'Add Cc
If (IsNull(Forms!frmMail!CCAddress)) Then
Else
    Set ObjOutlookRecip = .Recipients.Add(Forms!frmMail!CCAddress)
    ObjOutlookRecip.Type = olCC
End If
'set the subject, the body, and importance
.Subject = Forms!frmMail!Subject
.BodyFormat = olFormatHTML
        .HTMLBody = Forms!frmMail!MainText
.Importance = olImportanceHigh 'High
 
'Add the attachment to email message
If Not IsMissing(AttachmentPath) Then
    Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
'Resolve Name of recipient
For Each ObjOutlookRecip In .Recipients
    ObjOutlookRecip.Resolve
    If Not ObjOutlookRecip.Resolve Then
        objOutlookMsg.Display
    End If
    Next
    .Send
End With
MyRS.MoveNext
Loop
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub

The full tutorial is available on the link I gave above, I merged it with the help of stopher and replaced
Code:
.body= Forms!...
with
Code:
.BodyFormat = olFormatHTML
        .HTMLBody = Forms!frmMail!MainText

Simple really... just have to remember to enter the body in HTML code.

Also another thing, Step 11 in the tutorial is also the code to use in the button of a form to perfom the code as I discovered. I assume if the attachment is a variable then it needs to be referenced to a text box in the form rather than a file on the computer.
 

Users who are viewing this thread

Back
Top Bottom