Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending HTML Email body via Access/VBA

Status
Not open for further replies.

GregGood

Programmer
Jan 24, 2004
16
0
0
US
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
 
Can you show the code you are using now?

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
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$ = "COMFORT MUSIC DRAMATICALLY LOWERS PRICES!!!"
' 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.
MyBody.Close

' 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!
MyMail.Send

'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...
MailList.MoveNext

Loop

'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.

'MyOutlook.Quit
Set MyOutlook = Nothing

'MailList.Close
'Set MailList = Nothing
'db.Close
'Set db = Nothing

End Function
 
Have you tried .HTMLBody, rather than .Body?
 
Remou,
I have not tried .HTMLBody I will do that now. Thanks for the tip.
Greg Good
 
Remou,
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
 
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?
 
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,
Greg
 
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.
 
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?
Greg
 
I don't believe so. I am still wondering about Word.

This is what I used to test:

[tt]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>

<BODY>

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

<P>Hello!</P>
</BODY>
</HTML>[/tt]
 
Much simpler than what I've got. I'll make some changes and let you know.
Thanks,
Greg
 
Remou,
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,
Greg
 
Greg
Try this.

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

HTML File
[tt]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>

<BODY>

<IMG SRC="Orange.bmp"/>

<P>Hello!</P>
</BODY>
</HTML>[/tt]

Some Reading
[tt]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=" This has the same downside as (2).[/tt]

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top