VBA Save PDF to Specific Folder

partsbbw

New Member
Joined
Jan 18, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have the following code that will save an excel sheet as a PDF to the desktop by default. I would like to specify the folder that it will save to. The folder I need to save to, which may change is, C:\Users\User1\Desktop\Work.
Thank you!


VBA Code:
Function Save_PDF() As Boolean  ' Copies sheets into new PDF file
    Dim Thissheet As String, ThisFile As String, PathName As String
    Dim SvAs As String
 
Application.ScreenUpdating = False

    
' Get File Save Name
    Thissheet = ActiveSheet.Name
    ThisFile = ActiveWorkbook.Name
    PathName = ActiveWorkbook.Path
    SvAs = PathName & "\" & Thissheet & "_" & Format(Date, "yyyy-mm-dd") & ".pdf"
   
 
'Set Print Quality
    On Error Resume Next
    ActiveSheet.PageSetup.PrintQuality = 800
    Err.Clear
    On Error GoTo 0
 
' Instruct user how to send
    On Error GoTo RefLibError
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SvAs, Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
    On Error GoTo 0
    
SaveOnly:
    MsgBox "A copy of this sheet has been successfully saved as a  .pdf  file: " & vbCrLf & vbCrLf & SvAs & _
        "Review the .pdf document. If the document does NOT look good, adjust your printing parameters, and try again."
        
    Save_PDF = True
    GoTo EndMacro
    
RefLibError:
    MsgBox "Unable to save as PDF. Reference library not found."
    Save_PDF = False
EndMacro:
End Function
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Replace the SvAs = line with this code. It assumes the "Work" folder exists on the user's Desktop.
VBA Code:
    Dim DesktopFolder As String
    DesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    SvAs = DesktopFolder & "\Work\" & Thissheet & "_" & Format(Date, "yyyy-mm-dd") & ".pdf"
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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