VBA |导出为多页 PDF

VBA | Export To Multi-Page PDF

所以我有一个横向的作品sheet,我正在将其导出为 PDF。我可以通过选择 activesheetusedRange 来毫无问题地实现这一点,但是即使使用的范围在打印区域之外,内容也只会打印到一页。在这种情况下,我该如何让它打印多页 PDF?我的代码如下:

 Dim numSheets As Integer
     numSheets = UBound(SlotArray)
    
     For z = LBound(SlotArray) To UBound(SlotArray)
         Set attendSh = ThisWorkbook.Sheets(SlotArray(z))
         attendSh.Activate
         ThisWorkbook.ActiveSheet.UsedRange.Select
     Next z
    
     Select Case numSheets
         Case "0"
             ThisWorkbook.Sheets(Array(SlotArray(0))).Select
         Case "1"
             ThisWorkbook.Sheets(Array(SlotArray(0), SlotArray(1))).Select
         Case "2"
             ThisWorkbook.Sheets(Array(SlotArray(0), SlotArray(1), SlotArray(2))).Select
         Case "3"
             ThisWorkbook.Sheets(Array(SlotArray(0), SlotArray(1), SlotArray(2), SlotArray(3))).Select
         Case "4"
             ThisWorkbook.Sheets(Array(SlotArray(0), SlotArray(1), SlotArray(2), SlotArray(3), SlotArray(4))).Select
         Case "5"
             ThisWorkbook.Sheets(Array(SlotArray(0), SlotArray(1), SlotArray(2), SlotArray(3), SlotArray(4), SlotArray(5))).Select
         Case "6"
             ThisWorkbook.Sheets(Array(SlotArray(0), SlotArray(1), SlotArray(2), SlotArray(3), SlotArray(4), SlotArray(5), SlotArray(6))).Select
     End Select

         Application.PrintCommunication = False
         With ThisWorkbook.ActiveSheet.PageSetup
             .Orientation = xlLandscape
             .CenterHorizontally = True
            '.Zoom = 90
             .FitToPagesWide = 1
             .FitToPagesTall = 1
             .PrintComments = False
             .PrintGridlines = False
         End With

         Application.PrintCommunication = True

         Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
         FilePath & "\" & Year & " Monthly Attendance\" & "\" & Year & " " & Month & " Attendance.pdf", Quality:=xlQualityStandard, _
         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
         False

其中 slotArray 是 sheet 个名称的数组。

尝试将 PageSetUp 应用于数组中的每个 sheet。

    Dim numSheets As Long, Z As Long
    numSheets = UBound(SlotArray)
    
    Dim ws As Worksheet
    For Z = LBound(SlotArray) To UBound(SlotArray)
         Set ws = ThisWorkbook.Sheets(SlotArray(Z))
         With ws.PageSetup
             .Orientation = xlLandscape
             .CenterHorizontally = True
             .Zoom = False
             .FitToPagesWide = 1
             .FitToPagesTall = False
             .PrintComments = xlPrintNoComments
             .PrintGridlines = False
             .PrintArea = ws.UsedRange.Address
         End With
    Next Z
    ThisWorkbook.Sheets(SlotArray).Select
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Attendance.pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
    MsgBox "Printed " & vbLf & Join(SlotArray, vbLf)