Excell VBA 以指定的顺序使用 ExportAsFixedFormat 将一些工作表导出为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21631623/
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
Excell VBA export some worksheets to PDF with ExportAsFixedFormat in a specified order
提问by turbonate
I am trying to create a PDF report in Excel which consists of a TitleSheet and a ReportSheet. When I select the two sheets I need and use ExportAsFixedFormat to generate the PDF I can't seem to control the order which the sheets are added to the document. As of now, the TitleSheet ends up at the bottom of the ReportSheet... not very helpful.
我正在尝试在 Excel 中创建一个 PDF 报告,其中包含一个 TitleSheet 和一个 ReportSheet。当我选择我需要的两张工作表并使用 ExportAsFixedFormat 生成 PDF 时,我似乎无法控制将工作表添加到文档的顺序。截至目前,TitleSheet 最终位于 ReportSheet 的底部......不是很有帮助。
Here's my relevant code and some comments for things I tried that didn't work.
这是我的相关代码以及我尝试过但不起作用的一些评论。
Dim FilePath As String
FilePath = Application.GetSaveAsFilename(ReportSheet.Name & ".PDF", "PDF(*.pdf),*.pdf*", 1, "Save As PDF File")
If FilePath = "False" Then Exit Sub
TitleSheet.Select 'so it's the first in the DPF report, no worky
Dim PrintSheets(1) As Variant
PrintSheets(0) = TitleSheet.Name 'changed index order, no worky
PrintSheets(1) = ReportSheet.Name
Sheets(PrintSheets).Select 'select both sheets so they both print
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FilePath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ReportSheet.Activate 'select this sheet so it's visible at the end
I looked for a few other answers (Exporting multiple pages to PDF in a specific order) but no cigar yet. I have different column widths between the TitlePage and the ReportPage so I don't want to go through the headache of merging those. I also can't use a 3rd party PDF creator because a mojority of users don't have admin rights on their machines to install any additional software.
我寻找了其他一些答案(以特定顺序将多个页面导出为 PDF)但还没有雪茄。我在 TitlePage 和 ReportPage 之间有不同的列宽,所以我不想经历合并这些的麻烦。我也不能使用 3rd 方 PDF 创建者,因为大多数用户在他们的机器上没有管理员权限来安装任何其他软件。
采纳答案by turbonate
After much searching and testing I found out that ExportAsFixedFormat exports sheets based on the tab/index order. Which means I need to index ReportSheet after TitleSheet with this line of code:
经过多次搜索和测试,我发现 ExportAsFixedFormat 根据选项卡/索引顺序导出工作表。这意味着我需要使用以下代码行在 TitleSheet 之后索引 ReportSheet:
ReportSheet.Move after:=TitleSheet
Viola! Special thanks to Ken Puls: (http://www.excelguru.ca/forums/showthread.php?234-Reorder-pages-when-exporting-as-PDF&highlight=ExportAsFixedFormat)
中提琴!特别感谢 Ken Puls:(http://www.excelguru.ca/forums/showthread.php?234-Reorder-pages-when-exporting-as-PDF&highlight=ExportAsFixedFormat)