简体   繁体   中英

Printing multiple ranges and worksheets to one PDF file

I have a worksheet that often needs to be printed in a specific order of worksheets and ranges. What I'm trying to do is

  1. Print out specific worksheets and ranges in order to a single PDF file.
  2. Save the PDF file as a specific date to a certain folder.

The obstacle I'm running into is getting the numerous sheets and ranges printed into one PDF file, as currently each worksheet or range prints out to its own single PDF file.

I'm assuming there's a way to put all the necessary sheets and ranges in something like an array and then do a .PrintOut to that variable, however I haven't gotten this to work.

I'm using Excel 2010 and so I just use the "Adobe PDF" Printer.

How can I print multiple ranges and worksheets to a single .pdf?

To print more than one worksheet, you can put the worksheet names in an array like this

Sub PrintArrayOfWorksheets()

    Dim vaWorksheets As Variant

    vaWorksheets = Array("Sheet1", "Sheet2", "Sheet3")

    ThisWorkbook.Worksheets(vaWorksheets).PrintOut

End Sub

Printing to PDF has special problems. If you're using the "official" add-in for creating PDFs it will probably work as above (sorry I can't try it right now). In the past when I've worked with other printer drivers that print to PDF, I found that all (or at least most) of the PageSetup properties had to be identical on the sheets or it would print in multiple jobs. That means no shrink-to-fit and all the margins have to be the same. I can't remember all the properties that caused problems, just that those two definitely did and Orientation definitely didn't.

To limit the ranges that print, you need to set the print area on each sheet. You can use the PageSetup.PrintArea property to do it in code. But if doesn't change, it's probably just as well to do it manually one time and it will persist.

See code below. This was used to print multiple sheets in a custom order using a VBA Button. It prints to whatever printer is selected prior to clicking the button.

In essence what it does is selects the sheets that you want and rearranges them at the end of the workbook then prints them. after printing it reorders them to the original order.

Option Explicit
Private Sub Print_Sheets_InOrder_Click()
    Dim ary
    Dim orig() As String
    Dim a As Variant, fp As String, i As Variant
    ary = Array("Sheet1", "Sheet4", "Sheet3")
    fp = ActiveWorkbook.Path
    ReDim orig(1 To Sheets.Count)
    For i = 1 To Sheets.Count
        orig(i) = Sheets(i).Name
    Next i
    For Each a In ary
        Sheets(a).Move after:=Sheets(Sheets.Count)
    Next a
    ThisWorkbook.Worksheets(ary).PrintOut
    For Each a In orig
        Sheets(a).Move after:=Sheets(Sheets.Count)
    Next a
End Sub

Excel 2010 has a print to pdf feature. If each of your worksheets are set up to print the information that you want, then highlight the various worksheets you want to print to one pdf file, then go to "file" and "save-as" and pick save as type "pdf". It should print the worksheets you have highlighted as you have the print set up for each worksheet.

Here is an approach I have tried ... I print the worksheets to a PS file, then double-click on the PS file to generate a PDF. I know it is a workaround, but printing to PS is lightning quick. It requires you have Adobe Acrobat on your PC.

Dim GetPath, SavePath, ReportSuffix
GetPath = ActiveWorkbook.Path & "\"
ReportSuffix = Range("ReportName").Text & ".ps"
SavePath = GetPath & ReportSuffix

Dim PrintSheets

PrintSheets = Array("Exec1", "Intro1", "Intro2", "Intro3", "Intro4", "Intro5")

Sheets(PrintSheets).PrintOut Copies:=1, PrintTofile:=True, PrToFileName:=SavePath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM