导出为 PDF 脚本仅导出最后一个工作表



我有一个Excel VBA脚本,可以将工作表的内容导出为PDF。它适用于单个工作表,但现在我被要求让它包含第二个工作表。但是,使用下面的脚本现在仅包括第二个工作表,而不包括第一个工作表。谁能建议为什么不包括该对的第一个工作表?

Dim sheetsToPrint as Variant(1)
sheetsToPrint(0) = FirstWorksheet.Name
sheetsToPrint(1) = SecondWorksheet.Name
FirstWorksheet.PageSetup.PrintArea = Union(FirstWorksheet.Range("B2:I10"), FirstWorksheet.UsedRange).Address
With FirstWorksheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = -4
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
End With
SecondWorksheet.PageSetup.PrintArea = Union(secondPage.Range("B2:I10"), secondPage.UsedRange).Address
With SecondWorksheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = -4
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
End With
saveAndClose sheetsToPrint

saveAndClose 函数定义如下:

Private Sub saveAndClose(ByRef sheetsToPrint() As Variant)
Sheets(sheetsToPrint).Select
Dim primarySheetName As String
primarySheetName = CStr(sheetsToPrint(0))

Application.DisplayAlerts = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=outputDirectoryPath & ":" & primarySheetName & ".pdf"
Sheets(sheetsToPrint).Delete
Application.DisplayAlerts = True

End Sub

这是批处理过程的一部分,所以我没有复制我的所有代码,但这是当前导出仅包含工作表ToPrint(1)而不是工作表ToPrint(0)信息的PDF的部分。我整个下午都在为此苦恼,因为据我所知,这与互联网上如何将多张纸导出为 PDF 的每个示例相同。

谁能发现我哪里出错了?

你能这样设置它吗:

Dim sheetsToPrint As Sheets
Set sheetsToPrint = Sheets(Array("Blad1", "Blad2", "Blad3"))

然后在保存和关闭子中使用sheetsToPrint.select

相关内容

最新更新