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

标签 vba excel excel-2010 excel-2013

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

如果我手动导出并将其另存为 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/

相关文章:

vba - 查找彩色范围内的中位数

vba - 如何在 Excel 中查找单元格的所有依赖项(还有条件格式和数据验证)

vba - 编辑 Microsoft Office VBA 时,如何禁用弹出 "Compile error"消息?

excel - 引用整个数据透视表的公式?

vba - 在 Excel VBA 中的搜索中搜索值

vba - 列出附件的文件名

regex - excel-vba正则表达式模式

excel - 对 Excel 中具有相同文本值的行求和

excel - 如何计算 Excel 中的唯一值但忽略空白单元格?

excel - 使用每个文件打开 Excel 2010 的新实例