roh*_*l77 5 excel vba excel-vba excel-2010 excel-2013
我有将工作表格式化为所需的设置和布局的代码(横向一页高,横向)。当我运行代码(长宏的一部分)时,它会正确设置pagesetup的格式。
如果我手动导出并将其另存为pdf,则它将使用正确的页面设置,从而生成横向的一页PDF。但是,VBA所做的相同导出将产生几页长的纵向PDF。
我不知道为什么要这么做。我已经尝试了各种解决方案,例如在导出工作表之前选择了工作表,但都无济于事。
任何帮助表示赞赏。
代码如下:
Sub SaveAsPDF()
Sheets(ReportWsName).ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
[SaveFolderPath] & "\" & ReportWsName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub
Run Code Online (Sandbox Code Playgroud)
更新:
用于格式化pagesetup的代码(因为时间太长,我只添加了该子项的相关部分)
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
Run Code Online (Sandbox Code Playgroud)
我找到了似乎是解决方案:
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.Zoom = False
'.PrintArea = Worksheets(ReportWsName).UsedRange
.FitToPagesWide = 1
'.FitToPagesTall = 1
End With
Application.PrintCommunication = True
Run Code Online (Sandbox Code Playgroud)
我需要将 Application.PrintCommunication 部分添加到等式中。无论出于何种原因,如果我在没有它的情况下运行代码,Excel 都会覆盖我放置的设置。