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 Skilled (749 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
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.


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