Ludus H
Ludus H

Reputation: 239

How to print multiply sheets with selected page range into single PDF file with VBA

I have a workbook with 8 sheets and I planned to export all of them into single PDF file, but some sheets consist of only one page, and some of them have 2 or 3 pages to print. How to write it by using VBA? I tried to figure out some idea but all examples explained situation where multiple sheets is printed without taking in account number of pages on each sheets, is it possible? Thank you.

Upvotes: 0

Views: 1075

Answers (3)

Ludus H
Ludus H

Reputation: 239

I have controlled each Sheet, from Sheet1...Sheet8 thru menu option View -> Page Break View, where I adjusted by mouse print margins on right and lower side. Then plain routine printed it well, without hassle:

ThisWorkbook.Sheets(Array("Sheet1", "Sheet2",... "Sheet8")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\my_workbook.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
     IgnorePrintAreas:=False, OpenAfterPublish:=True

and it works, for now. I don't know is there some dependency of Excel version (2000 - 2013), because i can't check it. Thanks to all for visit and help.

Upvotes: 1

Here you go. Tested and working.

Sub SaveToPDF()

'This is where we will save the file to a PDF
   Sheets(Array("Sheet1", "Sheet2")).Select
   ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
   "insert file path here\insertfilename.pdf", _
   IgnorePrintAreas:=False, OpenAfterPublish:=True

End Sub

Upvotes: 1

cdatwork
cdatwork

Reputation: 56

If you want to print the entire workbook and all pages within to a single pdf, try:

Sub Print_Workbook()
    ActiveWorkbook.ExportAsFixedFormat xlTypePDF, "[C:\your_filename_here].pdf"
End Sub

Upvotes: 2

Related Questions