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.
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: