PDA

View Full Version : Sending emails with attachments in Access



kiwikiki718
04-10-2017, 05:14 PM
I have a database which contains the following table and fields :QryEmail with fields; emailadress,subject,Attachment. the attachment field contains the path of the pdf that I would like to attach to each person when sending the email. my issue is that I am unsure how to insert the correct coding to add the attachment to the email. Please help.

Here is the coding that I am using: I receive a error message stating "operation failed".

Dim Answer As VbMsgBoxResult
Answer = MsgBox("Are you Sure you want to Send Emails?", vbYesNo, "Send Emails")
If Answer = vbYes Then


Dim olLook As Object
Dim olNewEmail As Object
Dim strSql As String
Dim db As DAO.Database
Dim rst As DAO.Recordset



Set db = CurrentDb

strSql = "SELECT * from qryEmail"

Set rst = db.OpenRecordset(strSql, dbOpenDynaset)

Do While Not rst.EOF


Set olLook = CreateObject("Outlook.Application")
Set olNewEmail = olLook.CreateItem(0)
Set olNewEmail.SendUsingAccount = Outlook.Session.Accounts.Item(2)
Set myattachments = olNewEmail.Attachments




With olNewEmail 'Attach template
.To = rst.Fields("[EMailAddress]")
.SUBJECT = rst.Fields("[SUBJECT]")
.HTMLBody = "<font size=5 pt><font color=red><b><u>PLEASE READ THE ENTIRE ATTACHED FILE.</u></b></font>"
myattachments.Add = rst.Fields("ATTACH")
.SendUsingAccount = olNewEmail.SendUsingAccount

.Display
End With

rst.MoveNext
Loop

End If
End Sub

HiTechCoach
04-12-2017, 01:29 PM
What is the datatype for the field rst.Fields("ATTACH")? Is is text?

PS: You forgot to do the "clean up" after you were done with the recordset.

kiwikiki718
04-12-2017, 04:02 PM
Hi HitechCoach.Thanks for responding..yes the ATtach field is set to text. I have been trying to solve this issue going on 3 weeks now.

HiTechCoach
04-12-2017, 08:46 PM
I will be glad to help you get this working.

I have not seen Outlook Automation code like what you have posted. I will have to test it.

I will post back what I figure out from testing your code.

kiwikiki718
04-13-2017, 01:40 PM
Thanks so much for helping me. I will continue to work on this error in the meantime.

kiwikiki718
04-13-2017, 07:39 PM
I finally figured it out. Thanks everyone for your help.

HiTechCoach
04-14-2017, 11:15 AM
I finally figured it out. Thanks everyone for your help.

Great job figuring it out.

Would you please pay it forward by posting your solution.

Thank you.