2017-07-19 83 views
0

以下是我正在使用的代碼。這工作正常,並將前兩張轉換爲風景。但我不知道爲什麼它不會將sheet3和sheet4轉換爲橫向。有人可以幫我這個: -如何將多個工作表轉換爲PDF橫向

Public Sub SaveSheetsAsPDF() 

    Dim wksAllSheets As Variant 
    Dim wksSheet1 As Worksheet 
    Dim strFilename As String, strFilepath As String 

    'Set references up-front 
    Set wksSheet1 = ThisWorkbook.Sheets("Sheet1") 
    wksAllSheets = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4") 
     For Each sh In wksAllSheets 
     Sheets(sh).PageSetup.Orientation = xlLandscape 
    Next 
    'strFilepath = "C:\" 

    'Create the full Filename using cells D6, E6 and F6 
    With wksSheet1 
     'Assemble the string cell-by-cell, "D6 E6-F6" 
     strFilename = strFilepath & .Range("D6").Value & " " & _ 
            .Range("E6").Value & "-" & _ 
            .Range("F6").Value & ".pdf" 
    End With 

    'Save the Array of worksheets (which will be selected) as a PDF 
    ' ThisWorkbook. 
    Sheets(wksAllSheets).Select 
    ActiveSheet.ExportAsFixedFormat _ 
       Type:=xlTypePDF, _ 
       Filename:=strFilename, _ 
       Quality:=xlQualityStandard, _ 
       IncludeDocProperties:=True, _ 
       IgnorePrintAreas:=False, _ 
       OpenAfterPublish:=True 

    'Make sure all the worksheets are NOT left selected 
    wksSheet1.Select 

End Sub 
+0

FWIW,你的代碼爲我工作。 – YowE3K

+0

當時是否只有一個工作簿打開?我想知道'Sheets(sh).PageSetup.Orientation = xlLandscape'(它相當於'ActiveWorkbook.Sheets(sh).PageSetup.Orientation = xlLandscape')是否可能在錯誤的工作簿上運行。 (但是你正在使用Sheets(wksAllSheets)。稍後選擇',所以它**應該是OK。) – YowE3K

+0

你可以通過放置很多'Debug.Print表單(「Sheet2」)來調試它。PageSetup.Orientation &「|」 &Sheet(「Sheet3」)。PageSetup.Orientation'通過您的代碼聲明,看看它是否隨時寫出'2 | 2'(2爲橫向,1爲縱向),然後變回'2 | 1' - 如果確實如此,那意味着將其設置爲橫向工作正在發揮作用,但之後又有一些改變。 – YowE3K

回答

-1

請試試這個。在運行它之前,設置strFilename變量。

,如果它仍然失敗,出口每片手動,只是要找準失敗片

Public Sub SaveSheetsAsPDF() 

    Dim wksAllSheets As Sheets 
    Set wksAllSheets = Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")) 

    Dim sh As Worksheet 
    For Each sh In wksAllSheets 
     sh.PageSetup.Orientation = xlLandscape 
    Next 

    Dim strFilename As String 
    strFilename = "C:\Users\xxxxx\Desktop\Book1.pdf" ' set up path to test file 

    wksAllSheets.Select 
    ActiveSheet.ExportAsFixedFormat _ 
         Type:=xlTypePDF, _ 
        Filename:=strFilename, _ 
        Quality:=xlQualityStandard, _ 
     IncludeDocProperties:=True, _ 
      IgnorePrintAreas:=False, _ 
      OpenAfterPublish:=True 

    Sheets("Sheet1").Select 

End Sub