macro to save sheet as pdf on desktop regardless of user

skf786

Board Regular
Joined
Sep 26, 2010
Messages
155
Hi,

Want to save active sheet only, want to save as PDF and would like the file to open in pdf once it is saved, on the desktop, regardless of user. Thanks for your help in advance!!

Ive got very very limited know how of <acronym title="visual basic for applications">VBA</acronym>!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

CalcSux78

Well-known Member
Joined
Oct 15, 2013
Messages
1,120
There are a bunch of "Special Folders" that you can use. Read more here: https://msdn.microsoft.com/en-us/library/0ea7b5xe(v=vs.84).aspx
Code:
Sub DoPDF()
Dim sName$
Dim Path$


sName = ActiveSheet.Name


Path = CreateObject("WScript.Shell").specialfolders("Desktop")
On Error Resume Next
ThisWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=Path & "\" & sName & ".pdf", _
    OpenAfterPublish:=True
If Err.Number > 0 Then MsgBox "Error saving pdf."
End Sub
 
Upvote 0

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,801
Try this:
Code:
Sub Test()
    With ActiveSheet
        .ExportAsFixedFormat 0, Environ("USERPROFILE") & "\Desktop\" & _
            .Name & ".pdf", OpenAfterPublish:=True
    End With
End Sub
 
Upvote 0

skf786

Board Regular
Joined
Sep 26, 2010
Messages
155
calcsux! this is awesome! thanks so much. My desired file name is in cell e5. Plus I would like dd/mm/yyyy in the file name as well. :)
 
Last edited:
Upvote 0

skf786

Board Regular
Joined
Sep 26, 2010
Messages
155
Thanks Tetra 201! My desired file name is in cell e5. Plus I would like dd/mm/yyyy in the file name as well. :). Thanks in advance!
 
Upvote 0

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,801
Thanks Tetra 201! My desired file name is in cell e5. Plus I would like dd/mm/yyyy in the file name as well. :). Thanks in advance!
Here is the code as requested -- except: you cannot have a "/" in the file name.
Code:
Sub ExportPDF()
    With ActiveSheet
        .ExportAsFixedFormat 0, Environ("USERPROFILE") & "\Desktop\" & _
            .Range("E5").Value & "_" & Format(Date, "dd-mm-yyyy") & ".pdf", OpenAfterPublish:=True
    End With
End Sub
 
Last edited:
Upvote 0

skf786

Board Regular
Joined
Sep 26, 2010
Messages
155
Too Good! Thanks Tetra201. this might get me a promotion :)
 
Upvote 0

skf786

Board Regular
Joined
Sep 26, 2010
Messages
155
To make it even more user friendly: would like to add an 'email report' button which attaches the active sheet as pdf to a new email. Subject of the email shall be taken from cell p6 in the sheet. email address shall be taken from p7. However I would not like the email to be sent without the user pressing the send button. Would like to allow them to add text of their choice in the email. The email shall go through outlook of the respective users as the file is being shared with users in different locations.

Again, thanks so much for everything!
 
Upvote 0

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,801
To make it even more user friendly: would like to add an 'email report' button... The email shall go through outlook...
At the moment, I am unable to test any Outlook-related code. So, I cannot help you with this project. Hopefully somebody else will chime in on this.
 
Upvote 0

Forum statistics

Threads
1,191,500
Messages
5,986,921
Members
440,065
Latest member
JCH136

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
Top