【问题标题】:VBA | Export To Multi-Page PDFVBA |导出到多页 PDF
【发布时间】:2021-12-14 20:15:04
【问题描述】:

所以我有一个横向的工作表,我要导出为 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 是工作表名称数组。

【问题讨论】:

  • @KJ 我删除了它,但它仍然没有在每张纸上打印多页。

标签: excel vba pdf


【解决方案1】:

尝试将 PageSetUp 应用于数组中的每个工作表。

    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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 2015-08-03
    • 2019-06-09
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多