Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Sending HTML Email body via Access/VBA

Sending HTML Email body via Access/VBA

I am using a VBA module for sending email from Access through Outlook.  I am currently using a txt file for the email body but wish to us an HTML body.  I have tried simply using the HTML file but all I get is the HTML code in the body of the email (un-rendered).  Is there anyone out there who can help me?
Thank you,
Greg Good

RE: Sending HTML Email body via Access/VBA

Can you show the code you are using now?

Hope this helps,


Ignorance of certain subjects is a great part of wisdom

RE: Sending HTML Email body via Access/VBA

Hi Alex,
This is what I am currently using:
Public Function SndLPEmail()

Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String

Set fso = New FileSystemObject

 ' First, we need to know the subject.
 ' We can't very well be sending around blank messages...

' Subjectline$ = InputBox$("Please enter the subject line for this mailing.", _
'                 "We Need A Subject Line!")

 ' If there's no subject, call it a day.

If Subjectline$ = "" Then
    MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
        "Quitting...", vbCritical, "E-Mail Merger"
    Exit Function
End If
 ' Now we need to put something in our letter...
 BodyFile$ = "s:\gtg\Price_Drop_Apr07.htm"
'BodyFile$ = "s:\gtg\EmailLowerPrices.txt"
' BodyFile$ = InputBox$("Please enter the filename of the body of the message.", _
'             "We Need A Body!")

 ' If there's nothing to say, call it a day.

If BodyFile$ = "" Then
    MsgBox "No body, no message." & vbNewLine & vbNewLine & _
         "Quitting...", vbCritical, "I Ain't Got No-Body!"
    Exit Function
End If

 ' Check to make sure the file exists...
If fso.FileExists(BodyFile$) = False Then
    MsgBox "The body file isn't where you say it is. " & vbNewLine & vbNewLine & _
           "Quitting...", vbCritical, "I Ain't Got No-Body!"
    Exit Function
End If

   ' Since we got a file, we can open it up.
    Set MyBody = fso.OpenTextFile(BodyFile, ForReading, False, TristateUseDefault)

   ' and read it into a variable.
    MyBodyText = MyBody.ReadAll

   ' and close the file.

   ' Now, we open Outlook for our own device..
    Set MyOutlook = New Outlook.Application

 ' Set up the database and query connections

    Set db = CurrentDb()

    Set MailList = db.OpenRecordset("MyEmailAddresses")

 ' now, this is the meat and potatoes.
 ' this is where we loop through our list of addresses,
 ' adding them to e-mails and sending them.

    Do Until MailList.EOF

        ' This creates the e-mail
           Set MyMail = MyOutlook.CreateItem(olMailItem)
            ' This addresses it
            MyMail.To = MailList("Email")
            'This gives it a subject
            MyMail.Subject = Subjectline$
            'This gives it the body
            MyMail.Body = MyBodyText

            'If you want to send an attachment
            'uncomment the following line

            'MyMail.Attachments.Add "S:\gtg\Price_Drop_Apr07.doc", olByValue
            'MyMail.Attachments.Add "S:\gtg\Price_Drop_Apr07.pdf", olByValue

            ' To briefly describe:
            ' "c:\myfile.txt" = the file you want to attach
            ' olByVaue = how to pass the file.  olByValue attaches it, olByReference creates a shortcut.
            '      the shortcut only works if the file is available locally (via mapped or local drive)
            ' -1 = the position in the outlook message where to attachment goes.  This is ignored by most
            '      other mailers, so you might want to ignore it too.  Using -1 puts the attachment
            '      first in line.
            ' "My Displayname" = If you don't want the attachment's icon string to be "c:\myfile.txt" you
            '      can use this property to change it to something useful, i.e. "4th Qtr Report"

            'This sends it!

            'Some people have asked how to see the e-mail
            'instead of automaticially sending it.
            'Uncomment the next line
            'And comment the "MyMail.Send" line above this.

'            MyMail.Display

    'And on to the next one...


 'Cleanup after ourselves

Set MyMail = Nothing

'Uncomment the next line if you want Outlook to shut down when its done.
'Otherwise, it will stay running.

Set MyOutlook = Nothing

'Set MailList = Nothing
'Set db = Nothing

End Function

RE: Sending HTML Email body via Access/VBA

Have you tried .HTMLBody, rather than .Body?

RE: Sending HTML Email body via Access/VBA

I have not tried .HTMLBody  I will do that now.  Thanks for the tip.
Greg Good

RE: Sending HTML Email body via Access/VBA

I used .HTMLBody and I did get the document to render in the Email. The only problem that remains is that I have a picture at the top of the page that is not coming through. When I browse the html page, the picture is there but all I have is a place holder in the email (no picture). Any thoughts?
Thanks for your help,
Greg Good

RE: Sending HTML Email body via Access/VBA

It works for me with Office 2000. Just thinking ... is the image local? Does the image work when you build an email manually? What version of Office are you using?

RE: Sending HTML Email body via Access/VBA

Hi Remou,
I have 2000 pro at home and xp pro at work and both are failing.  I've tried different things with the image.  I have it in a directory and it is also imbedded in the Word doc that I'm using to create the html page.  How would I build the email page maually?  I tried opening a new email page and then cutting and pasting the html page into the emal while the html page was rendered.  The picture is still missing.  I have a feeling that it's going to be something simple but it's sure got me for now.
Thanks again for your help,

RE: Sending HTML Email body via Access/VBA

That, I think, may be where your problem is; Word can do dreadful things to HTML. Open the HTML File with notepad and manually change the image source to the name of the directory where the image is stored.

RE: Sending HTML Email body via Access/VBA

I went in, as you suggested, and checked the address of the picture in the html code; it looks ok and works fine when I open the page with a browser.  I'm wondering if the picture must be in a particular directory in order for Outlook to see it as it is rendering the page?

RE: Sending HTML Email body via Access/VBA

I don't believe so. I am still wondering about Word.

This is what I used to test:



<IMG SRC="C:\Docs\Pic.bmp" Width="80" Height="50">


RE: Sending HTML Email body via Access/VBA

Much simpler than what I've got.  I'll make some changes and let you know.

RE: Sending HTML Email body via Access/VBA

Well, by doing what you suggested I was able to see the picture in the outgoing email (big step) and when I sent the email to myself I was able to see the picture in the received email (another big step)!  When I sent the email to someone else, however, the picture was, again, nowhere to be seen.  It seems like the picture must be embedded in the in the email source so that it can be displayed no matter where it goes.
Getting closer.
Thank again,

RE: Sending HTML Email body via Access/VBA

Try this.



   MyMail.Attachments.Add "C:\Docs\Pic.bmp", olByValue
   'This gives it the body
   MyMail.HTMLBody = MyBodyText



<IMG SRC="Orange.bmp"/>


Some Reading
3. You could attach the images to the message and reference them with a relative path, so <img src="img1.gif"/> instead of <img src="http://servername/images/img1.gif"/>. This has the same downside as (2).


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close