Geographos
Geographos

Reputation: 1456

Attaching PDF file to Outlook email

I would like to add a .pdf file in my Outlook email, which is sent via VBA Excel.

My full Excel code looks pretty much like this:

 Sub Confirmationemail()     
 MsgBox ("The confirmation email will be sent now")

 Dim OutlookApp As Outlook.Application
 Dim OutlookMail As Outlook.MailItem

 Dim fs As Worksheet, bs As Worksheet
 Dim Filename As String, Name As String, Name2 As String, Name3 As String, Reason As String
 Dim Cost As String, PathFileName As String
 Dim linecount2 As Long

 ChDir ThisWorkbook.Path & "\"

 Set fs = Sheets("Frontsheet")
 Set bs = Sheets("BoM")
 linecount2 = 1

 Name = fs.Range("D10")
 Name2 = fs.Range("D18")
 Name3 = fs.Range("D38")

 If fs.Range("D38").Value = 3 Then
     Reason = fs.Range("K8")
 ElseIf fs.Range("D38").Value = 4 Then
     Reason = fs.Range("P4")
 Else
     Reason = fs.Range("K4")
 End If

 Filename = Name & "_" & Name2

 Set OutlookApp = New Outlook.Application
 Set OutlookMail = OutlookApp.CreateItem(olMailItem)

 With OutlookMail
     .BodyFormat = olFormatHTML
     .Display
     .HTMLBody = "The job is ready. See the PDF version in the attachment"

     .To = "[email protected]; [email protected] "
     PathFileName = ThisWorkbook.Path & "\" & Filename & ".pdf"
     .CC = "[email protected]; [email protected];"
     .BCC = "[email protected]"
     .Subject = Filename & "- Audit"
     '.Attachments.Add PDFFile
     myattachments.Add PathFileName
     '.Attachments.Add Application.ActiveWorkbook.FullName
     '.Send
 End With

 End Sub

The best hint I found: How to attach exported pdf file to Outlook mail using Excel VBA? but it refers to attaching the already exported PDF document. Incorporating some pieces of code was unsuccessful.

Some solution here: Attach PDF and send email via Outlook but it refers to the specified cells only.

Other hints I found:
Excel VBA attaching print area as PDF.
Attach both pdf and excel files to an email on single click in VBA. https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_winother-mso_2010/attaching-a-pdf-file-in-vba-generated-email-in/527de6b4-66e6-4aa5-85b8-267a59ea6a7f

Upvotes: 1

Views: 6323

Answers (1)

0m3r
0m3r

Reputation: 12499

It’s not myattachments.Add PathFileName it should be .Attachments.Add PathFileName

See Attachments.Add method (Outlook)

Upvotes: 1

Related Questions