Saving Print Ranges

omagoodness

Board Regular
Joined
Apr 17, 2016
Messages
59
I have a named print range on a sheet ("TipsPaid") that I want to save to pdf using VBA and command button. The code I am using (below) will select the range and open in a pdf document but does not save it to a file. I have been over 4 hours trying to figue out why it will not save. What am I doing wrong? I'm sure it is something simple but I don't see it. Please help.
VBA Code:
Option Explicit
Private Sub cmdSave_Click()
'declaring variables

'Setting range to be saved
Dim PRng As Range
Set PRng = Range("TipsPaid")

'path to save folder
Dim myPath As String
myPath = Application.ThisWorkbook.Path & "\" & "Tip-out Records"

'setting file name
Dim SDate As String, filename As String
SDate = Application.WorksheetFunction.Text(Cells(3, 17), "dd mmm yyyy")
filename = "Week Ending" & " " & SDate & ".pdf"

'Save Location
 Dim SaveLocation As String
 SaveLocation = myPath & " \ " & filename
'Save as pdf
PRng.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
             "SaveLocation", Quality:=xlQualityStandard, _
             IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
it's Savelocation without double quotes !!!
VBA Code:
PRng.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
             SaveLocation, Quality:=xlQualitySt....
 
Upvote 0
it's Savelocation without double quotes !!!
VBA Code:
PRng.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
             SaveLocation, Quality:=xlQualitySt....
Thank you for your reply. When I removed the quotes, I got an error.
 

Attachments

  • error.JPG
    error.JPG
    91.5 KB · Views: 2
Upvote 0
SaveLocation = myPath & " \ " & filename WRONG
SaveLocation = myPath & "\" & filename GOOD
there are no spaces in that separator
 
Upvote 0
SaveLocation = myPath & " \ " & filename WRONG
SaveLocation = myPath & "\" & filename GOOD
there are no spaces in that separator
Thanks. I'm still getting a Runtime 1004 message at the saving part. Below is my code with the 2 suggested corrections as well as a pic showing where the debug takes me. Man, I sure hate when this happens.
VBA Code:
Option Explicit
Private Sub cmdSave_Click()
'declaring variables

'Setting range to be saved
Dim PRng As Range
Set PRng = Range("TipsPaid")

'path to save folder
Dim myPath As String
myPath = Application.ThisWorkbook.Path & "\" & "Tip-out Records"

'setting file name
Dim SDate As String, filename As String
SDate = Application.WorksheetFunction.Text(Cells(3, 17), "dd mmm yyyy")
filename = "Week Ending" & " " & SDate & ".pdf"

'Save Location
 Dim SaveLocation As String
 SaveLocation = myPath & "\" & filename
'Save as pdf
PRng.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
             SaveLocation, Quality:=xlQualityStandard, _
             IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


End Sub
 

Attachments

  • error.JPG
    error.JPG
    49.4 KB · Views: 0
Upvote 0
is there a "Tip-out Records" subdirectory under the actual path, thus the path thisworkbook.path & "\Tip-out Records", that exists ?
do you get an error if you add that 2nd line in your macro ? (is that "application" necessary ?)

VBA Code:
myPath = Application.ThisWorkbook.Path & "\" & "Tip-out Records"
chdir mypath
 
Upvote 0
short version
VBA Code:
Private Sub cmdSave_Click()
     Dim MyFilename As String

     MyFilename = ThisWorkbook.Path & "\" & "Tip-out Records\Week ending_" & Format(Cells(3, 17), "dd mmm yyyy hhmmss") & ".pdf" 'for test also with hours minutes seconds
     MsgBox MyFilename, vbInformation, UCase("Saving pdf")
     Range("TipsPaid").ExportAsFixedFormat xlTypePDF, MyFilename, OpenAfterPublish:=True

End Sub
 
Upvote 0
short version
VBA Code:
Private Sub cmdSave_Click()
     Dim MyFilename As String

     MyFilename = ThisWorkbook.Path & "\" & "Tip-out Records\Week ending_" & Format(Cells(3, 17), "dd mmm yyyy hhmmss") & ".pdf" 'for test also with hours minutes seconds
     MsgBox MyFilename, vbInformation, UCase("Saving pdf")
     Range("TipsPaid").ExportAsFixedFormat xlTypePDF, MyFilename, OpenAfterPublish:=True

End Sub
short version
VBA Code:
Private Sub cmdSave_Click()
     Dim MyFilename As String

     MyFilename = ThisWorkbook.Path & "\" & "Tip-out Records\Week ending_" & Format(Cells(3, 17), "dd mmm yyyy hhmmss") & ".pdf" 'for test also with hours minutes seconds
     MsgBox MyFilename, vbInformation, UCase("Saving pdf")
     Range("TipsPaid").ExportAsFixedFormat xlTypePDF, MyFilename, OpenAfterPublish:=True

End Sub
Still getting the same error. Yes, the subfolder "Tip-out Record" does exist. If I add a message box with the SaveLocation, the path and file name is exactly as it should be. The problem seems to exist in the export to pdf line.
 

Attachments

  • err.JPG
    err.JPG
    76.4 KB · Views: 2
Upvote 0
does that named range exist ?
VBA Code:
Sub test()
     Dim c, MyFilename
     MyFilename = ThisWorkbook.Path & "\" & "Tip-out Records\Week ending_" & Format(Cells(3, 17), "dd mmm yyyy hhmmss") & ".pdf"     'for test also with hours minutes seconds
     MsgBox MyFilename, vbInformation, UCase("Saving pdf")
     ChDir ThisWorkbook.Path & "\" & "Tip-out Records"     'test existence of this subdirectory
     Set c = Range("TipsPaid")     'teste existence of this named range
     Range("TipsPaid").ExportAsFixedFormat xlTypePDF, MyFilename, OpenAfterPublish:=True
End Sub
 
Upvote 0
Solution
does that named range exist ?
VBA Code:
Sub test()
     Dim c, MyFilename
     MyFilename = ThisWorkbook.Path & "\" & "Tip-out Records\Week ending_" & Format(Cells(3, 17), "dd mmm yyyy hhmmss") & ".pdf"     'for test also with hours minutes seconds
     MsgBox MyFilename, vbInformation, UCase("Saving pdf")
     ChDir ThisWorkbook.Path & "\" & "Tip-out Records"     'test existence of this subdirectory
     Set c = Range("TipsPaid")     'teste existence of this named range
     Range("TipsPaid").ExportAsFixedFormat xlTypePDF, MyFilename, OpenAfterPublish:=True
End Sub
Thank you. I stuck that in at the bottom of my code and it saved a .pdf in the right folder but it was totally blank. I guess I just need to tweak a bit to include the range. I will ge tback to you on what happens. I have to be away from my computer for a while but I will get back to you. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,873
Messages
6,127,454
Members
449,383
Latest member
DonnaRisso

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