0 votes
in VBA by Beginner (10 points)

I purloined Ryan's code from another website. The code saves a range of cells selected by the user to a pdf and allows user to name that pdf.

Problem: I spec'd a default "save" path with this line:

strfile = "C:\Users\......\_2019-2020\2019-20 2nd Semester\Quant PDF Dump\\Selection_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"

(Note: the ellipsis indicates portions of path statement hidden for privacy purposed. The path is correct.)

The code, however, defaults to "My Documents" for the save location instead of the final folder spec'd in the path, Quant PDF Dump\\

Thanks so much for taking time to read and respond to this query!


'SUBROUTINE: PrintSelectionToPDF
'DEVELOPER: Ryan Wells
'DESCRIPTION: Print your currently selected range to a PDF

Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant

If Selection.Count = 1 Then
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
Set ThisRng = Selection
End If

'Prompt for save location'
strfile = "C:\Users\.....\_2019-2020\2019-20 2nd Semester\Quant PDF Dump\\Selection_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"
'"C:\Users\.....\_2019-2020\2019-20 2nd Semester\Quant PDF Dump\" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf" _

myfile = Application.GetSaveAsFilename _
(InitialFileName:=strfile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save as PDF")

If myfile <> "False" Then 'save as PDF
ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True


MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"

End If

End Sub

1 Answer

+1 vote
by Expert (805 points)
selected by
Best answer
strfile = "...\Quant PDF Dump\\Selection_" & ...

Did you intend to include two back-slash chars before Selection?

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.