issues saving formats to PDF file

cirugio

Board Regular
Joined
Mar 30, 2010
Messages
130
Hoping someone can assist. I am have a workbook with multiple sheets for which I want to format each sheet to fit 1 page with an orientation of Landscape and then save all sheets to one PDF file.

I have found some VBA code online which allows me to set my desired Page Setup (i.e. fit to one page and landscape) and then call another subroutine to save all the sheets to one PDF file. The page setup routine works fine but I noticed that when it calls the routine to save to a PDF file, all the page settings do not carry over and appear to default to what they were originally (portrait & don't fit to 1 page).

I am not sure why this is happening. Hoping someone can help for I think I have exhausted everything . I am including the code i have now below. Thanks for your time in advance.

Code:
[B]Private Sub Format_Worksheets_Fit1Page()[/B]:confused:

 strTargetWorkbook = "BalSht Template.xlsm"
 Dim sh As Worksheet, wb As Workbook
 Set wb = Workbooks(strTargetWorkbook)
 
 For Each sh In Workbooks(strTargetWorkbook).Worksheets
 
  '--- PageSetup to fit to 1 page & make Landscape ---

 Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftMargin = Application.InchesToPoints(0.7)
        .RightMargin = Application.InchesToPoints(0.7)
        .TopMargin = Application.InchesToPoints(0.75)
        .BottomMargin = Application.InchesToPoints(0.75)
        .HeaderMargin = Application.InchesToPoints(0.3)
        .FooterMargin = Application.InchesToPoints(0.3)
        .PrintComments = xlPrintNoComments
        .Orientation = xlLandscape
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    Application.PrintCommunication = True
    


  Next sh
  
'--- call print routine to save to PDF format

Call Save_File_to_PDF
  
End Sub

[B]Private Sub Save_File_to_PDF()[/B]

'---- Create a dynamic array which will house all the worksheet names in the workbook to be printed
    Dim myArray() As Variant
    Dim i As Integer
    For i = 1 To Sheets.Count
        ReDim Preserve myArray(i - 1)
        myArray(i - 1) = i
    Next i
    
'--- select all the worksheets within the array so can save to 1 PDF file

    Sheets(myArray).Select    '---- this command seems to reset the PageSetting back to the defaults for some reason


   '--- save file ---
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="F:\balsht test.pdf", _
     Quality:=xlQualityStandard, IncludeDocProperties:=True, _
     IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top