Print PDF and Email

BT63

New Member
Joined
Aug 9, 2023
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I am trying to print a defined print area from a single worksheet, save as a PDF and then email. I have tried, but can't seem to get it to actually send the email. Below is my code. Any help would be much appreciated. It would also be nice if it would save as the active worksheet name. Thank you!

Sub PrintToPDF_Email()
ChDir "C:\Users\turne\Documents"
'Print to PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\turne\Documents\Sales Report March.pdf", OpenAfterPublish:=True

'Declare Variables
Dim EmailApp As Object
Dim EmailItem As Object
Dim myAttachments As Object

'Set Variables
Set EmailApp = CreateObject("Outlook.application")
Set EmailItem = EmailApp.CreateItem(0)
Set myAttachments = EmailItem.Attachments

'Specify Email Items and Add Attachment
With EmailItem
.To = "barryturner@cookiesgac.com"
.Subject = "Sales Report of Fruits"
.Body = "Please find the PDF attachment of the Excel file"
.Attachments.Add "C:\Users\turne\Documents\Sales Report March.pdf"
.send
'.Display


End With
Set EmailItem = Nothing
Set EmailApp = Nothing

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
What happens exactly when you try running your code?

If you get an error, which one and on which line?
 
Upvote 0
Run-time error '-2147467259(80004005)': Automation error Unspecified error
Error 440
 
Upvote 0
Doesn't show any line. It just occurs when I change the code from display to send
 
Upvote 0
Does it help if you display your email prior to entering the values for To, Subject, etc., and then send it? So maybe like this?

VBA Code:
'Specify Email Items and Add Attachment
With EmailItem
    .Display
    .To = "barryturner@cookiesgac.com"
    .Subject = "Sales Report of Fruits"
    .Body = "Please find the PDF attachment of the Excel file"
    .Attachments.Add "C:\Users\turne\Documents\Sales Report March.pdf"
    .send
End With
 
Upvote 0
Wow, you're a genius! That worked perfectly. Thank you so much for your help.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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