Full Version: Sending a HTML email
UtterAccess Discussion Forums > Microsoft® Access > Access Q and A
BlueMeanie
I have a table holding contact information. One Y/N field is a reference to whether a contact should recieve a monthly newsletter.

This is going to be done using a HTML email.

What I want is to automate the sending of all emails on the push of a button. I have automated email generation and sending before, but have not created a HTML text body through VBA before.

Does anybody have knowledge of this that could pass on?

Thanks

Paul
good_patriot
The "SendObject" macro choice can perform this exact procedure. Under the "To" category you can list as many emal addresses as you want , they need only be seperated by semi-colons. It also allows for text messaging. When I use this I have to shut off a few system windows that pop up using the SetWarnings command.
BlueMeanie
Thanks for the reply.

The issue is the HTML body text not the sending to multiple users.

To do the multiple sending I will loop through a recordset of the contacts that require the newsletter, this way the list can change from month to month without ever having to alter the code.
danishani
Hi there,

I am bussy with this issue recently, so far I know u can attach an HTML document to your mail and it will show up automaticly in your body in Outlook or Groupwise if its configured right.

Another approach for attach HTML to body by code is something like this:

CODE


   Set myOLApp = CreateObject("Outlook.Application")
   Set myItem = myOLApp.CreateItem(olMailItem)


   myItem.HTMLBody = "<HTML><H2>My HTML page.</H2><BODY>My body.</BODY></HTML>"
    'Item displays HTML message.
   myItem.Display


U need to have Microsoft Outlook Object ticked to get this code working.

HTH
Daniel
BlueMeanie
Thanks for the info Dani, I will give this a go sometime today.

Cheers

Paul
BlueMeanie
Have created the code that i felt should work but have hit a snag.

I want to use a text file to hold the HTML as opposed to entering it via VBA. This is so the boss can just alter the text as he sees fit without going into the database internals.

To do this I thought the use of a pathname for the file would do the job, so I created a string variable and set it to contain the pathname. The string variable is then called.

In relation to your code this would be - myItem.HTMLBody = myVariable

I am getting a compile error on the specific line of code stating 'Object Required'

Why is this when I have previously declared the variable???

See code below if needed. Many Thanks

Paul

_________________________________________________
Private Sub cmd_SendNewsletter_Click()

Dim db As Database
Dim contactRec As Recordset

Dim objOutlook As New Outlook.Application
Dim objMessage As MailItem

Dim strBody As String 'Holds the mail body to be sent
Dim strName As String 'Holds the name of the recipient
Dim strHTML As String 'Holds the HTML text to be included in the body

'Set Database & Recordset objects
Set db = CurrentDb()
Set contactRec = db.OpenRecordset("tbl_Contacts")

'Sets pathway for location of HTML text file
Set strHTML = "c:\My Documents\Newsletter.txt"

'Loops throught each contact record
While Not contactRec.EOF

'Performs if contact requires a newsletter
If contactRec("Newsletter") = True Then
Set objMessage = objOutlook.createitem(olMailItem)
With objMessage
.To = contactRec("Email")
.Subject = contactRec("FirstName") & " " & contactRec("Surname") & _
"'s " & "Denca Newsletter"
.HTMLBody = strHTML
.Send
End With
End If

contactRec.MoveNext

Wend

Set contactRec = Nothing
Set objOutlook = Nothing
Set objMessage = Nothing

End Sub



BlueMeanie
Got the code running now, I had put :-

Set strHTML = "c:\My Documents\Newsletter.txt"

......instead of :-

strHTML = "c:\My Documents\Newsletter.txt"

Idiot!


danishani
Glad u got it going thumbup.gif

Daniel
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.