Help with VBA to Save as PDF

Kap61

New Member
Joined
Jun 28, 2012
Messages
13
I want to save certain sheets as a PDF file with a specific path, have a default file name with the option to edit the file name which also opens the PDF file.

Seems to work with desired path and option to change default file name, but the PDF file is never saved and the code continues to the end where the excel file is saved.

Any help to tweak the following would be appreciated.



Sub SavePDF()
'
' SavePDF Macro
'


'
Dim FileAndLocation As Variant
Dim strPathLocation As String
Dim strFilename As String
Dim strPathFile As String


strPathLocation = Worksheets("Master").Range("D3").Value
strFilename = Worksheets("Master").Range("D2").Value
strPathFile = strPathLocation & strFilename


Sheets("DPV Test Log I-1-1 ").Select
ActiveWindow.ScrollWorkbookTabs Sheets:=16
Sheets(Array("DPV Test Log I-1-1 ", "DPV Test Log I-1-1A", "DPV Test Log I-1-2", _
"DPV Test Log I-1-3 #1 ", "DPV Test Log I-1-3 #2 ", "DPV Test Log I-1-3 #3 ", _
"DPV Test Log I-1-12 Lunch room", "DPV Test Log I-1-20", "DPV Test Log I-1-23 #1 " _
, "DPV Test Log I-1-23 #2 ", "DPV Test Log I-1-34", "DPV Test Log I-1-58", _
"DPV Test Log I-1-88", "DPV Test Log I-1-101", "DPV Test Log I-1-134", _
"DPV Test Log F2-10", "DPV Test Log F-6-45", "DPV Test Log P-1-1", _
"DPV Test Log S-3-3", "DPV Test Log T-1-3", "DPV Test Log T-1-9")).Select
Sheets("DPV Test Log I-1-1 ").Activate
FileAndLocation = Application.GetSaveAsFilename _
(InitialFileName:=strPathLocation & strFilename, _
filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
ActiveWindow.ScrollWorkbookTabs Sheets:=-16
Sheets("Master").Select
ActiveWorkbook.Save
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
From what I can tell you never tell it to save it as a pdf

Code:
 Dim LocationName As String
     fName = ActiveSheet.Range("A2").Value
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
             "C:\Users\Jdavis\Dropbox\Quality Control\Aggregates\Stockpile Gradation\" & fName, Quality:=xlQualityStandard, _
             includeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

The first code is an example of saving a pdf that I use. Below is a generic vba to save as a pdf
Code:
Sheets("Sheet1").Range("A1:H20").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="C:\Users\marks\Documents\Saved PDF.pdf"
 
Upvote 0
Try this

Code:
Sub SavePDF()
    Dim FileAndLocation  As Variant
    
'Select sheets
    Sheets(Array("DPV Test Log I-1-1 ", "DPV Test Log I-1-1A", "DPV Test Log I-1-2", _
        "DPV Test Log I-1-3 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  ", "DPV Test Log I-1-3 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=2]#2[/URL]  ", "DPV Test Log I-1-3 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=3]#3[/URL]  ", _
        "DPV Test Log I-1-12 Lunch room", "DPV Test Log I-1-20", "DPV Test Log I-1-23 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  ", _
        "DPV Test Log I-1-23 [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=2]#2[/URL]  ", "DPV Test Log I-1-34", "DPV Test Log I-1-58", _
        "DPV Test Log I-1-88", "DPV Test Log I-1-101", "DPV Test Log I-1-134", _
        "DPV Test Log F2-10", "DPV Test Log F-6-45", "DPV Test Log P-1-1", _
        "DPV Test Log S-3-3", "DPV Test Log T-1-3", "DPV Test Log T-1-9")).Select
    
'Select file and location
    FileAndLocation = Application.GetSaveAsFilename(InitialFileName:="new.pdf", FileFilter:="PDF files, *.pdf", Title:="Save PDF File")
    If FileAndLocation = False Then Exit Sub
    
'save file as pdf
    [COLOR=#0000ff]ActiveSheet[/COLOR].ExportAsFixedFormat Type:=xlTypePDF, Filename:=[COLOR=#0000ff]FileAndLocation[/COLOR], Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Sheets("Master").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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