MS Access Module to Send Email

tuktuk

Well-known Member
Joined
Nov 13, 2006
Messages
856
Hey there,

I currently have code that sends an email that i found online. It looks to a txt file for the content of the body of the mail. This is currently functioning.

I am trying to add a Hyperlink to the body of the email.....but unfortunatly i cannot figure out how to add the hyperlink to a txt file......or would it be easier to add the link to the vba code that creates the msg.

thanks
tuk

Code:
Public Sub 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$ = "DATA IS READY"
'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 Sub
End If
 
' Now we need to put something in our letter...
Const FILE_PATH As String = "C:\MyPath\"
Dim FullPath As String
strFullPath = FILE_PATH
Dim strDate As String
Dim strReport As String

strFile = strFullPath & "MYFILE.xlsm"
 
BodyFile$ = "C:\MYEMAILBODYPATH\EmailBody.txt"

'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 Sub
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 Sub
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()
 
'gather distroList
Set MailList = db.OpenRecordset("qry_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.
' and we add them to the RECIPIENTS collection
 
Set MyMail = MyOutlook.CreateItem(olMailItem)
 
Do Until MailList.EOF
 
' This creates the e-mail
' This addresses it
 
 MyMail.Recipients.Add MailList("Email")
 
'This gives it a subject
MyMail.Subject = Subjectline$
 
'This gives it the body
MyMail.Body = MyBodyText
 

'And on to the next one...
MailList.MoveNext
 
Loop
MyMail.Attachments.Add strFile, olByValue, 1, "My Displayname"
 
MyMail.Send

'Cleanup after ourselves
Set MyMail = Nothing
Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing
 
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,214,621
Messages
6,120,563
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top