简体   繁体   中英

Problems with landscape orientation when converting sheets in an Excel workbook to pdf files VBA

I'm trying to convert each sheet in a workbook to pdf files with "landscape layout", where the paper is laying down. But the paper orientation is definitely not landscape. Can someone help me out?

It seems to be a general problem, however I can't find any solution which works for me.

Here is the code.

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

Please, try the next code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM