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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I send Email Merging (HTML) in MS ACCESS 2000 thorugh Outlook

Status
Not open for further replies.

spop

Technical User
Jun 15, 2003
5
AU
Hello I am creating a Database to send many emails (HTML with pictures etc...) I have created this code for a module, however it works and does what I need however it only sends text and I need to send HTML pages full of graphics and features. Please help anyone.

Option Compare Database
Option Explicit
' You need to declare a reference to the Outlook library, and the filesystemobject.
' this is not as hard as it sounds.
'
' Look in the menu above, and click Tools, then select References
'
' Scroll down the list until you see
' Microsoft Scripting Runtime -- and put a check next to it (if one is not there already)
'
' Microsoft Outlook Object Library -- check that.
' There will be some version number there as well; it doesn't matter.
' This will work with Outlook98 and Outlook2000


Public Function SendEMail()

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$ = 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

'This sends it!
MyMail.Send


'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
 
The MailItem object has an HTMLBody property. This may do what you want.


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Hi what version do you need to have that object library... in mine the Outlook.MailItem doesnt have any further objects....
 
Sorri I checked the object it says the MailItem Property HTMLBody Is As String. So therefore it will only do TEXT not any graphics....does anyone ahve any ideas ? please.....
 
HTML is text.

I've just created an email containing the winlogo.gif file. On saving to disk and opening in notepad all the info was there, just in text form:

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML><HEAD>
<META http-equiv=Content-Type content=&quot;text/html; charset=iso-8859-1&quot;>
<META content=&quot;MSHTML 6.00.2800.1170&quot; name=GENERATOR></HEAD>
<BODY><B>From:</B> Andy Watt<BR><B>Sent:</B> 16 June 2003 14:02<BR><B>To:</B> 
Administrator<BR><B>Subject:</B> a test<BR>
<DIV><FONT face=Tahoma size=2><SPAN 
class=790190013-16062003>Testing</SPAN></FONT></DIV>
<DIV><FONT face=Tahoma size=2></FONT> </DIV>
<DIV><FONT face=Tahoma size=2><IMG alt=&quot;Win Logo&quot; hspace=0 
src=&quot;cid:790190013@16062003-10e0&quot; align=baseline border=0></FONT></DIV>
<DIV><FONT face=Tahoma size=2></FONT> </DIV>
<DIV><SPAN class=790190013-16062003><FONT face=Tahoma 
size=2>testing</FONT></SPAN></DIV></BODY></HTML>


Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;Why does my program keep showing error messages every time something goes wrong?&quot;
 
Hello I dont know how to modify the code to use it for the MailItem.HTMLbody property can someone give me a hand:


this is what i have so far and instead of having just the plain text mail sending, i need to send HTML emails.. thankx

-----------------------------------------------------------
Option Compare Database
Option Explicit
' You need to declare a reference to the Outlook library, and the filesystemobject.
' this is not as hard as it sounds.
'
' Look in the menu above, and click Tools, then select References
'
' Scroll down the list until you see
' Microsoft Scripting Runtime -- and put a check next to it (if one is not there already)
'
' Microsoft Outlook Object Library -- check that.
' There will be some version number there as well; it doesn't matter.
' This will work with Outlook98 and Outlook2000


Public Function SendEMail()

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$(&quot;Please enter the subject line for this mailing.&quot;, &quot;We Need A Subject Line!&quot;)

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

If Subjectline$ = &quot;&quot; Then
MsgBox &quot;No subject line, no message.&quot; & vbNewLine & vbNewLine & &quot;Quitting...&quot;, vbCritical, &quot;E-Mail Merger&quot;
Exit Function
End If

' Now we need to put something in our letter...

BodyFile$ = InputBox$(&quot;Please enter the filename of the body of the message.&quot;, &quot;We Need A Body!&quot;)

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

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

' Check to make sure the file exists...
If fso.FileExists(BodyFile$) = False Then
MsgBox &quot;The body file isn't where you say it is. &quot; & vbNewLine & vbNewLine & &quot;Quitting...&quot;, vbCritical, &quot;I Ain't Got No-Body!&quot;
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(&quot;MyEmailAddresses&quot;)

' 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(&quot;email&quot;)

'This gives it a subject
MyMail.Subject = Subjectline$

'This gives it the body
MyMail.Body = MyBodyText

'This sends it!
MyMail.Send


'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
 
Hi spop,
i've used your code and it seems to work great. Thanks!!

But...

Most times my system hangs when i use it. I mean, the routine starts but Access freezes while sending.
I have to go in the Task Manager, stop outlook, so that Access recovers but without sending.
I tried to set the task priority of Access in the Task Manager to the lowest grade and only in this case it seems to work correctly...

Any suggestions to correct this problem without managing the tasks manually every time??

My configuration is:
Win XP pro SP1
office 2000
512 mb ram
a lot of hard disk free space ;)
 
I should have found the solution and now it seems to work correctly always.
I cut out from the code these two rows:
Set MyMail = Nothing
Set MyOutlook = Nothing

and &quot;activated&quot; this:
MyOutlook.Quit

I don't know why, i'm not a real technician, but it's good :))

Bye all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top