Saving a PDF after attacing it to an email

AcornNut

Board Regular
Joined
Aug 19, 2014
Messages
51
I'm having trouble with saving the active worksheet as a PDF, sending it as an attachement in an email, and RETAINING the email in a designated folder. Also having trouble having the file name as "worksheet.name" & date ".pdf." I'd like the date added to the end of the name, but can't seem to get it to work. I found this code online, and it's been working ok as far as sending the worksheet as a PDF attachment, but it WILL NOT SAVE in the designated folder! Can someone help.
What I need is..
1) The current date added to the file name (so each new pdf will be saved and not overwritten)
2) The PDF to ACTUALLY be saved and not deleted after attaching/emailing.
Please help!!!
Thanks
Here's what I have....

Sub SendPDF()
Dim strPath As String, strFName As String
Dim OutApp As Object, OutMail As Object

'Create PDF of active sheet only
strPath = Environ$("FILE PATH HERE ex.c:\folder1\folder2\folder3") & Application.PathSeparator & strFName
strFName = ActiveSheet.Name & ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
strPath & strFName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "EMAIL ADDRESS HERE"
.CC = ""
.BCC = ""
.Subject = "Supply Order"
.Attachments.Add strPath & strFName
.Send End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
One thing I notice is:

Code:
[COLOR=#006400]'Create PDF of active sheet only[/COLOR]
[COLOR=#333333]strPath = Environ$("FILE PATH HERE ex.c:\folder1\folder2\folder3") & Application.PathSeparator & strFName [/COLOR]
[COLOR=#333333]strFName = ActiveSheet.Name & ".pdf"[/COLOR]

You are trying to use the variable strFName before you set its value... you would need to use it in this order instead:

Code:
[COLOR=#333333]strFName = ActiveSheet.Name & ".pdf"[/COLOR]
[COLOR=#333333]strPath = Environ$("FILE PATH HERE ex.c:\folder1\folder2\folder3") & Application.PathSeparator & strFName [/COLOR]

I imagine it's not saving in the correct folder because you have not specified to save it or where to save it...

You need to add something like this to the code:

Code:
ChDir "C:\Some Folder\"
ActiveWorkbook.SaveAs Filename:= strFName
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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