Save Excel Sheet as PDF to user's desktop

cperrak1

New Member
Joined
Sep 10, 2023
Messages
3
Hello,

I wrote the code to save my spreadsheet as a PDF file. The Macro is working correctly, but I've been asked to make it dynamic and save it to the desktop regardless of user. Any suggestions?

Sub PrintTRENDPDF()

Worksheets("TREND").Activate

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\nperraki\OneDrive - Myriad Genetics\Desktop\TREND.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

Worksheets("MACRO TILES").Activate
Worksheets("MACRO TILES").Cells(1, 1).Select

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this:
VBA Code:
Sub PrintTRENDPDF()

Worksheets("TREND").Activate

user = Application.Username

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\" & user & "\nperraki\OneDrive - Myriad Genetics\Desktop\TREND.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

Worksheets("MACRO TILES").Activate
Worksheets("MACRO TILES").Cells(1, 1).Select

End Sub
 
Upvote 0
Try this:
VBA Code:
Sub PrintTRENDPDF()

Worksheets("TREND").Activate

user = Application.Username

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\" & user & "\nperraki\OneDrive - Myriad Genetics\Desktop\TREND.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

Worksheets("MACRO TILES").Activate
Worksheets("MACRO TILES").Cells(1, 1).Select

End Sub
Try this:
VBA Code:
Sub PrintTRENDPDF()

Worksheets("TREND").Activate

user = Application.Username

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\" & user & "\nperraki\OneDrive - Myriad Genetics\Desktop\TREND.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

Worksheets("MACRO TILES").Activate
Worksheets("MACRO TILES").Cells(1, 1).Select

End Sub
Hello,

Thank you for the reply. I'm not quite there yet. "C:\Users\nperraki\OneDrive - Myriad Genetics\Desktop..." is the path to the desktop of the computer that I'm working off of. Once the excel sheet is converted to PDF, it needs to be saved down to any user's local desktop. Thanks again.
 
Upvote 0
Try (untested)...

VBA Code:
Sub PrintTRENDPDF()
    Dim strFolderName As String


    Worksheets("TREND").Activate
    strFolderName = CreateObject("wscript.shell").Specialfolders("Desktop") & "\TREND.pdf"


    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                    strFolderName, _
                                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                                    IgnorePrintAreas:=False, OpenAfterPublish:=False

    Worksheets("MACRO TILES").Activate
    Worksheets("MACRO TILES").Cells(1, 1).Select

End Sub
 
Upvote 0
Try (untested)...

VBA Code:
Sub PrintTRENDPDF()
    Dim strFolderName As String


    Worksheets("TREND").Activate
    strFolderName = CreateObject("wscript.shell").Specialfolders("Desktop") & "\TREND.pdf"


    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                    strFolderName, _
                                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                                    IgnorePrintAreas:=False, OpenAfterPublish:=False

    Worksheets("MACRO TILES").Activate
    Worksheets("MACRO TILES").Cells(1, 1).Select

End Sub
Thank you for your reply,

I was able to figure it out!

Sub PrintTRENDPDF()

Dim Ws As Worksheet
Dim sPath As String
Dim sFile As String
Set Ws = ActiveSheet
sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
sFile = "TREND"

Worksheets("TREND").Activate

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sPath & "\" & sFile, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False

Worksheets("MACRO TILES").Activate
Worksheets("MACRO TILES").Cells(1, 1).Select

End Sub
 
Upvote 0
Solution
Happy that you have it working but the bits in red below (and I have tested now)
Rich (BB code):
sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
sFile = "TREND"

Worksheets("TREND").Activate

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sPath & "\" & sFile

do nothing different to

VBA Code:
strFolderName = CreateObject("wscript.shell").Specialfolders("Desktop") & "\TREND.pdf"
 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                                    strFolderName

EDIT: you are also declaring and setting Ws
VBA Code:
Dim Ws As Worksheet
Set Ws = ActiveSheet

and then not using Ws, so those lines are obsolete
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,024
Members
449,092
Latest member
ikke

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