将Excel工作簿中的图纸转换为pdf文件时的横向取向问题VBA

问题描述

我正在尝试将工作簿中的每张工作表转换为具有“横向布局”的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

解决方法

请尝试下一个代码:

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