PDA

View Full Version : Error trying to attach file to email



kiwikiki718
04-11-2017, 07:45 PM
I have been trying to run a code that will attach a pdf document to a email but have been unsuccessful. I keep receiving "Operation failed" when trying to attach the file. Please help

Logit
04-11-2017, 10:43 PM
.
.
Paste into Routine Module:




Sub EmailWithOutlook()
Dim oApp As Object
Dim oMail As Object
Dim ThisFile As String

ThisFile = "C:\Users\My\Desktop\Report.pdf" '<-----------------------------------------------Location and Name of file to attach
Application.ScreenUpdating = False

'Create and show the Outlook mail item
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
'
.To = "To Field"
.Subject = "Subject Field"
.Body = "Please review the attachment."
.Attachments.Add ThisFile '<-------------------------------------------------------
.Display '<-- comment out this line if you don't first want to view the email before sending.
'.Send '<-- uncomment this line if you want the email to auto send
End With


Application.ScreenUpdating = True

Set oMail = Nothing
Set oApp = Nothing

End Sub


Work with MS Outlook ..

kiwikiki718
04-12-2017, 09:18 AM
Thanks for your response. I currently have a table with the field "ATTACH" that stores the path of all the pdfs. below is the coding that Iam using however when it gets to the attachment line I receive the error message.

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