Excel VBA 将选定的工作表导出为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20750854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:28:27  来源:igfitidea点击:

Excel VBA to Export Selected Sheets to PDF

excelvbapdfexport

提问by thistledownjohn

I'm using the following code to export selected sheets from Excel 2010 to a single pdf file...

我正在使用以下代码将选定的工作表从 Excel 2010 导出到单个 pdf 文件...

ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select

ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="C:\temp.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

My problem is that it only exports the first sheet. Any ideas?

我的问题是它只导出第一张纸。有任何想法吗?

回答by Gary's Student

Once you have Selected a group of sheets, you can use Selection

一旦您选择了一组工作表,您就可以使用选择

Consider:

考虑:

Sub luxation()
    ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
    Selection.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="C:\TestFolder\temp.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
End Sub

EDIT#1:

编辑#1:

Further testing has reveled that this technique depends on the group of cells selectedon each worksheet. To get a comprehensive output, use something like:

进一步的测试表明,该技术取决于在每个工作表上选择的单元格组。要获得全面的输出,请使用以下内容:

Sub Macro1()

   Sheets("Sheet1").Activate
   ActiveSheet.UsedRange.Select
   Sheets("Sheet2").Activate
   ActiveSheet.UsedRange.Select
   Sheets("Sheet3").Activate
   ActiveSheet.UsedRange.Select

   ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
   Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
      "C:\Users\James\Desktop\pdfmaker.pdf", Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
      True
End Sub

回答by asp8811

I'm pretty mixed up on this. I am also running Excel 2010. I tried saving two sheets as a single PDF using:

我对此很困惑。我也在运行 Excel 2010。我尝试使用以下方法将两张工作表保存为单个 PDF:

    ThisWorkbook.Sheets(Array(1,2)).Select
    **Selection**.ExportAsFixedFormat xlTypePDF, FileName & ".pdf", , , False

but I got nothing but blank pages. It saved both sheets, but nothing on them. It wasn't until I used:

但我除了空白页什么也没有。它保存了两张工作表,但没有任何内容。直到我使用了:

    ThisWorkbook.Sheets(Array(1,2)).Select
    **ActiveSheet**.ExportAsFixedFormat xlTypePDF, FileName & ".pdf", , , False

that I got a single PDF file with both sheets.

我得到了一个包含两张纸的 PDF 文件。

I tried manually saving these two pages using Selection in the Options dialog to save the two sheets I had selected, but got blank pages. When I tried the Active Sheet(s) option, I got what I wanted. When I recorded this as a macro, Excel used ActiveSheet when it successfully published the PDF. What gives?

我尝试使用“选项”对话框中的“选择”手动保存这两页以保存我选择的两张纸,但得到了空白页。当我尝试 Active Sheet(s) 选项时,我得到了我想要的。当我将此记录为宏时,Excel 在成功发布 PDF 时使用了 ActiveSheet。是什么赋予了?

回答by HattrickNZ

this is what i came up with as i was having issues with @asp8811 answer(maybe my own difficulties)

这是我想出的,因为我在@asp8811 答案中遇到了问题(也许是我自己的困难)

' this will do the put the first 2 sheets in a pdf ' Note each ws should be controlled with page breaks for printing which is a bit fiddly ' this will explicitly put the pdf in the current dir

'这将把前 2 张纸放在 pdf 中'注意每个 ws 应该用分页符控制打印,这有点繁琐'这将明确地将 pdf 放在当前目录中

Sub luxation2()
    Dim Filename As String
    Filename = "temp201"



Dim shtAry()
ReDim shtAry(1) ' this is an array of length 2
For i = 1 To 2
shtAry(i - 1) = Sheets(i).Name
Debug.Print Sheets(i).Name
Next i
Sheets(shtAry).Select
Debug.Print ThisWorkbook.Path & "\"


    ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "/" & Filename & ".pdf", , , False

End Sub