I have code that formats a worksheet for the desired setup and layout (one page wide and tall in the landscape). When I run the code (part of a long macro), it correctly formats the page.
If I manually export and save it as a pdf, then it uses the correct page setup, creating a one-page PDF file that is in the landscape. However, the same export done by VBA creates a PDF file that lasts several pages in the portrait as well.
I canβt understand why he is doing this. I tried various solutions, such as selecting a worksheet before exporting it, but all to no avail.
Any help is appreciated.
The code is as follows:
Sub SaveAsPDF() Sheets(ReportWsName).ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ [SaveFolderPath] & "\" & ReportWsName, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _ False End Sub
UPDATE:
The code used to format the page set (since it is quite long, I only add the corresponding section of this subdirectory)
Private Sub CreateNewReport(ProvisionCode As String, TimeFrom As Date, TimeTo As Date) ... other code here... 'Format report to create the desired layout With Worksheets(ReportWsName) 'Delete unnecessary data and format the rest .Range("A:B,D:D,F:G,J:M,O:O,Q:S").Delete Shift:=xlToLeft .Range("A:F").EntireColumn.AutoFit .Range("C:C, E:F").ColumnWidth = 30 With .Range("G:G") .ColumnWidth = 100 .WrapText = True End With 'Insert standard formating header form Reporting template .Rows("1:2").Insert wsReportTemplate.Range("1:3").Copy .Range("A1") .Range("A2") = "Notes Report for " & ProvisionCode & " (" & TimeFrom & " - " & TimeTo & ")" 'Insert standard formating footer form Reporting template wsReportTemplate.Range("A6:G7").Copy .Range("A" & .UsedRange.Rows.Count + 2) 'Ensure all data is hard coded .UsedRange.Value = .UsedRange.Value 'Format Print Area to one Page With ActiveSheet.PageSetup .PrintArea = Worksheets(ReportWsName).UsedRange .Orientation = xlLandscape .FitToPagesWide = 1 End With End With End Sub
source share