Need help sending an automated email in outlook using VBA while attaching a PDF from a folder location. Below is what I have thus far. I'm stuck on the part to attach the PDF
Sub ViolationProcessingTest()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<BODY style = font-size:11pt;font-family:Calibri>" & _
"Hello, <br><br> Please process the attached violations."
On Error Resume Next
With OutMail
.To = "[email protected]"
.CC = "[email protected]"
.Subject = "Violations Processing"
.Display
.HTMLBody = strbody & .HTMLBody
.Attachments.Add = "Folder path location with PDF"
End With
On Error GoTo 0
Set OutMail = Nothing
End Sub
CodePudding user response:
Firstly, it is a good idea to get rid of the On Error Resume Next
line - errors are raised for a reason, otherwise you won't even know why or where your code is failing.
Secondly, your code is almost OK, you just need to get rid of =
(it is a method call, not a property assignment) and specify a fully qualified PDF file name:
.Attachments.Add "c:\SomeFolder\TheFile.PDF"
CodePudding user response:
The Attachments.Add method (not a property) ceates a new attachment in the Attachments collection. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. So, the code should like that:
With OutMail
.To = "[email protected]"
.CC = "[email protected]"
.Subject = "Violations Processing"
.HTMLBody = strbody & .HTMLBody
.Attachments.Add filePathToYourFile
.Display
End With
You may find the How To: Create and send an Outlook message programmatically article helpful.