vba 将多张工作表保存为 .pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14404650/
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
Save multiple sheets to .pdf
提问by guthrie
I have a reporting spreadsheet that grabs some data from a database and forms three sheets of summary report information. I want to run the spreadsheet automatically from the command line, and have it automatically save all three reporting sheets as a PDF file(s).
我有一个报告电子表格,它从数据库中获取一些数据并形成三张汇总报告信息。我想从命令行自动运行电子表格,并让它自动将所有三个报告表保存为 PDF 文件。
At first I thought I could have a VBA macro on the sheet do this by a series of "print as PDF", but that requires an intermediary interactive dialog box to specify the output file name. Then I find that I can just save as pdf, and the macro can set the output file name. However this creates three separate files, and I have to then later put them together externally to merge them.
起初我以为我可以在工作表上有一个 VBA 宏,通过一系列“打印为 PDF”来完成此操作,但这需要一个中间交互式对话框来指定输出文件名。然后我发现我可以直接保存为pdf,宏可以设置输出文件名。但是,这会创建三个单独的文件,然后我必须在外部将它们放在一起以合并它们。
(It is odd that save as pdf only saves one sheet, where other save modes seem to save the entire workbook.)
(奇怪的是,另存为 pdf 只能保存一张工作表,而其他保存模式似乎可以保存整个工作簿。)
Yes, there are tools for merging the three files later, but I want to know if there is some easy way to get Excel to save multiple sheets together as one pdf file.
是的,有一些工具可以稍后合并这三个文件,但我想知道是否有一些简单的方法可以让 Excel 将多个工作表保存为一个 pdf 文件。
I print now by a series of things like:
我现在通过一系列的东西来打印:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ...
Could I do instead a single statement something like (pseudo-code):
我可以做一个像(伪代码)这样的单一语句:
ThisWorkbook.Sheets(1,2,3,4).ExportAsFixedFormat Type:=xlTypePDF, ...
回答by Tim Williams
Start by selecting the sheets you want to combine:
首先选择要合并的工作表:
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\tempo.pdf", Quality:= xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
回答by Peter Albert
Similar to Tim's answer - but with a check for 2007 (where the PDF export is not installed by default):
类似于 Tim 的回答 - 但检查 2007 年(默认情况下未安装 PDF 导出):
Public Sub subCreatePDF() If Not IsPDFLibraryInstalled Then 'Better show this as a userform with a proper link: MsgBox "Please install the Addin to export to PDF. You can find it at http://www.microsoft.com/downloads/details.aspx?familyid=4d951911-3e7e-4ae6-b059-a2e79ed87041". Exit Sub End If ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=ActiveWorkbook.Path & Application.PathSeparator & _ ActiveSheet.Name & " für " & Range("SelectedName").Value & ".pdf", _ Quality:=xlQualityStandard, IncludeDocProperties:=True, _ IgnorePrintAreas:=False, OpenAfterPublish:=True End Sub Private Function IsPDFLibraryInstalled() As Boolean 'Credits go to Ron DeBruin (http://www.rondebruin.nl/pdf.htm) IsPDFLibraryInstalled = _ (Dir(Environ("commonprogramfiles") & _ "\Microsoft Shared\OFFICE" & _ Format(Val(Application.Version), "00") & _ "\EXP_PDF.DLL") <> "") End Function
回答by Armand
I recommend adding the following line after the export to PDF:
我建议在导出为 PDF 后添加以下行:
ThisWorkbook.Sheets("Sheet1").Select
(where eg. Sheet1
is the single sheet you want to be active afterwards)
(其中例如Sheet1
是您之后想要激活的单张纸)
Leaving multiple sheets in a selected state may cause problems executing some code. (eg. unprotect doesn't function properly when multiple sheets are actively selected.)
将多个工作表保留在选定状态可能会导致执行某些代码时出现问题。(例如,当主动选择多个工作表时,取消保护无法正常工作。)
回答by Pam S.
In Excel 2013 simply select multiple sheets and do a "Save As" and select PDF as the file type. The multiple pages will open in PDF when you click save.
在 Excel 2013 中,只需选择多个工作表并执行“另存为”并选择 PDF 作为文件类型。单击保存时,多个页面将以 PDF 格式打开。