Although this questions was already asked I did not find a real answer for it.
I need to programmatically select worksheets in a workbook and save them to PDF file WITHOUT creating temporary workbook and copying selected worksheets to it.
Alternatively how do create workbook without displaying it - that is, in memory and then on HD? This is again may help to solve the first question...
Try this:
Sub SaveToPdf()
Dim ws As Worksheet
Dim sfName As String
For Each ws In Worksheets
sfName = ws.Name
sfName = "C:\Misc\" & sfName & ".pdf"
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sfName, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next ws
End Sub
You'd need to make sure your print areas on each sheet were set first I think
Related
I have the below script to export each excel sheet to a separate PDF page. It works fine, except for one small issue. I need the range Columns A:H to fit on one PDF page. How can I update the below script to make sure I export all information on one page for the specified columns?
Sub ExportToPDFs()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
ActiveSheet.Columns("A:H").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\My Folder\" & nm & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next ws
End Sub
I would set the print area to A:H and set scale to fit to 1 page wide and 1 page high through the Page Setup part of the Ribbon. Then the following code should work without having to mess with the scaling through VBA.
Sub ExportToPDFs()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
nm = ws.Name
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\My Folder\" & nm & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Next ws
End Sub
Hi have 50+ sheets in my excel workbook and i want to save them individual pdf files.
I also want to save them in specific folder and want to name these files from certain cell.
I got the basic of basic, saving them individually to desktop, but having trouble saving to certain folder and name part.
Sub Save_As_PDF_To_Desktop_4()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> "Instructions" And sh.Visible = True Then sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
CreateObject("WScript.Shell").specialfolders("Desktop") & "\" & sh.Name & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Next sh
End Sub
Could someone assist me on this? Thanks!
I'm trying to loop through all active sheets and save them as separate PDFs.
dim ws as worksheet
dim path as string
...
For Each ws In ActiveWindow.SelectedSheets
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
It kind of works:
A PDF file is created for each selected sheet in the correct folder... but...
The contents of those PDF files are all the same. It is the Active Sheet being printed each time but with a different filename.
How to fix this? Keep in mind, I only want selected sheets to print.
EDIT: I'm too new to upvote. Thanks for your answers!
You need to Select the sheet before printing out.
Just add the command ws.Select right before ws.ExportAsFixedFormat and it will work:
For Each ws In ActiveWindow.SelectedSheets
ws.Select '<-- the only thing you have to add.
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
Apart for this:
Tip 1: to avoid seeing the sheets jumping over and over, you can add Application.ScreenUpdating = False before the loop starts and then setting it back to Application.ScreenUpdating = True at the end of the loop.
Tip 2: for user friendliness, you can get the currently active sheet at the beginning of the loop with Dim currentlySelectedSheet As Worksheet: Set currentlySelectedSheet = ActiveSheet and when the loop ends, you just select it back with currentlySelectedSheet.Select so that the user won't see anything change in their screen when running your macro.
Option Explicit
Sub Save_SelectedSheets_AsPdf()
Dim ws As Worksheet
Dim path As String
Dim actSheet As Worksheet
'...
Set actSheet = ActiveSheet
For Each ws In ActiveWindow.SelectedSheets
ws.Select
ws.ExportAsFixedFormat _
xlTypePDF, _
Filename:=path & ws.Name, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenafterPublish:=False
Next
actSheet.Select
End Sub
Sub Create_5_Sheets_and_name_them()
Dim iCt As Integer
For iCt = 2 To 5
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Sh" & iCt
Range("A1").Value = ActiveSheet.Name
Next iCt
End Sub
you can specify it like this before you export it to pdf
I am just trying to use VBA to create create/save individual excel sheets as individual PDF files.
I came across this online:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next wks
End With
End Sub
However, I'm getting the following error:
Run-time error '5':
Invalid procedure call or argument
For someone not 100% familiar with VBA, what's invalid?
Thanks
As I said in my comment, your code runs fine (Win-10, Excel-365). I first:
Saved the .xlsm file to insure Spath existsSelected an area on each worksheet to insure it will be PDF'ed correctly
I have a workbook with many worksheets. I would like to save as two-page PDFs, where the first page is Worksheet 1, and the second page is Worksheets 2-x. My code currently only allows me to save individual PDFs for each worksheet in the workbook. I am wondering what to add to it to make it do this. Can anyone share some advice?
Thanks!
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
You are enumerating through the worksheets and doing your save inside that loop. That is why you are getting one PDF per worksheet. Try using just workbook instead of ActiveWorkbook.Worksheets.
Gah. It was staring me in the face the whole time. I amended the code to include a selection, and named the second worksheet ws.Name. Final script looks like this:
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
Sheets(Array("Sheet1", ws.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
Thanks for your help everyone!