gpt4 book ai didi

vba - excel vba 未正确将 pagesetup 导出为 pdf

转载 作者:行者123 更新时间:2023-12-02 07:24:18 47 4
gpt4 key购买 nike

我有代码可以将工作表格式化为所需的设置和布局(横向一页宽和高)。当我运行代码(长宏的一部分)时,它会正确格式化页面设置。

如果我手动导出并将其另存为 pdf,则它会使用正确的页面设置,生成横向的一页 PDF。然而,通过 VBA 完成的相同导出会生成一个长达数页的纵向 PDF。

我不明白为什么要这样做。我尝试了各种解决方案,例如在导出之前选择工作表,但都无济于事。

感谢任何帮助。

代码如下所示:

Sub SaveAsPDF()
Sheets(ReportWsName).ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
[SaveFolderPath] & "\" & ReportWsName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub

更新:

用于格式化页面设置的代码(因为它相当长,我只添加该子项的相关部分)

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

最佳答案

我找到了似乎是解决方案:

Application.PrintCommunication = False
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.Zoom = False
'.PrintArea = Worksheets(ReportWsName).UsedRange
.FitToPagesWide = 1
'.FitToPagesTall = 1
End With
Application.PrintCommunication = True

我需要将 Application.PrintCommunication 部分添加到方程式中。无论出于何种原因,如果我在没有 Excel 的情况下运行代码,Excel 都会覆盖我所做的设置。

关于vba - excel vba 未正确将 pagesetup 导出为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24862064/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com