【问题标题】:Problems with landscape orientation when converting sheets in an Excel workbook to pdf files VBA将 Excel 工作簿中的工作表转换为 pdf 文件 VBA 时出现横向问题
【发布时间】:2020-11-04 14:17:28
【问题描述】:

我正在尝试将工作簿中的每张工作表转换为具有“横向布局”的 pdf 文件,其中纸张正在放置。但是纸张方向绝对不是横向的。 有人可以帮帮我吗?

这似乎是一个普遍的问题,但是我找不到任何适合我的解决方案。

这里是代码。

Sub Test()
Application.ScreenUpdating = False
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
    sPath = .Path & "\"
    For Each wks In .Worksheets
        sFile = wks.Name & ".pdf"
        wks.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sPath & sFile
        Application.PrintCommunication = False
        With wks.PageSetup
            .Orientation = xlLandscape
            .Zoom = False
            .CenterHorizontally = True
            .CenterVertically = True
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            '.BottomMargin = 0
            '.TopMargin = 0
            '.RightMargin = 0
            '.LeftMargin = 0
        End With
        Application.PrintCommunication = True
    Next wks
End With
Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 尝试使用“Microsoft Print to PDF”打印机并在上面打印。它将保持工作表方向...

标签: excel vba pdf landscape


【解决方案1】:

请尝试下一个代码:

Sub ExportAsPdfLandscape()
 Dim wks As Worksheet, Path As String, strPName As String, strShName As String

  Path = "Folder path where to be saved\" ' end it in "\", please!
  
  strPName = Application.ActivePrinter   'this records the current printer

  For Each wks In ActiveWorkbook.Worksheets
      wks.PageSetup.Orientation = xlLandscape
      strShName = Path & wks.Name & ".pdf"

      wks.PrintOut , , 1, , ActivePrinter:="Microsoft Print to PDF", _
           Printtofile:=False, collate:=True, PrToFileName:=strShName, Ignoreprintareas:=True
  Next
  
  Application.ActivePrinter = strPName   'return to the former current printer
End Sub

【讨论】:

  • @FaneDure,, , 1, , 是什么意思?
  • @User123456789:表示副本数。 PrintOut 的主题是:expression.PrintOut (From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, IgnorePrintAreas)...它实际上是一个打印机设置,代码使用“Microsof Print to PDF”在文件上打印...
猜你喜欢
  • 2020-06-27
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
相关资源
最近更新 更多