简体   繁体   English

将多个范围和工作表打印到一个PDF文件

[英]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. 打印出特定的工作表和范围,以便单个PDF文件。
  2. Save the PDF file as a specific date to a certain folder. 将PDF文件另存为特定文件夹的特定日期。

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. 我遇到的障碍是将大量的纸张和范围打印成一个PDF文件,因为目前每个工作表或范围都打印到它自己的单个PDF文件。

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. 我假设有一种方法可以将所有必要的工作表和范围放在类似于数组的内容中,然后对该变量执行.PrintOut,但是我没有让它工作。

I'm using Excel 2010 and so I just use the "Adobe PDF" Printer. 我正在使用Excel 2010,所以我只使用“Adobe PDF”打印机。

How can I print multiple ranges and worksheets to a single .pdf? 如何将单个范围和工作表打印到单个.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. 打印到PDF有特殊问题。 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). 如果您使用“官方”插件来创建PDF,它可能会如上所述(抱歉,我现在无法尝试)。 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. 过去当我使用其他打印到PDF的打印机驱动程序时,我发现所有(或至少大多数)PageSetup属性必须在工作表上相同,否则它将在多个作业中打印。 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. 我不记得导致问题的所有属性,只是那两个肯定做了,而Orientation肯定没有。

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. 您可以使用PageSetup.PrintArea属性在代码中执行此操作。 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. 这用于使用VBA按钮以自定义顺序打印多个工作表。 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. Excel 2010具有print to pdf功能。 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". 如果您的每个工作表都设置为打印所需的信息,则突出显示要打印到一个pdf文件的各种工作表,然后转到“文件”和“另存为”并选择另存为“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. 这是我尝试过的方法...我将工作表打印到PS文件,然后双击PS文件生成PDF。 I know it is a workaround, but printing to PS is lightning quick. 我知道这是一种解决方法,但打印到PS很快就会闪现。 It requires you have Adobe Acrobat on your PC. 它要求您在PC上安装Adobe Acrobat。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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