【问题标题】:Excel: Export to PDF not updatingExcel:导出为 PDF 不更新
【发布时间】:2015-05-01 03:47:37
【问题描述】:

我正在尝试让 Excel 将我的表格导出为 PDF 文件。问题是,它总是用相同的数据导出 PDF。我添加更多条目还是删除它们都没有关系。

这是我用我的初学者 VBA 技能整理的代码:

Sub export_pdf()

Dim area As String
area = Range("B3:H1048576").End(xlUp).SpecialCells(xlCellTypeConstants, 3).Address

With ActiveSheet.PageSetup
    .PrintTitleRows = "$3:$3"
End With

ActiveSheet.PageSetup.PrintArea = "" & area

With ActiveSheet.PageSetup
    .CenterHeader = "Käyttöpäiväkirja"
    .CenterFooter = "Sivu &P / &N"
    .LeftMargin = Application.InchesToPoints(0.590551181102362)
    .RightMargin = Application.InchesToPoints(0)
    .TopMargin = Application.InchesToPoints(0.748031496062992)
    .BottomMargin = Application.InchesToPoints(0.748031496062992)
    .HeaderMargin = Application.InchesToPoints(0.31496062992126)
    .FooterMargin = Application.InchesToPoints(0.31496062992126)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlPortrait
    .Draft = False
    .PaperSize = xlPaperA4
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = 100
    .PrintErrors = xlPrintErrorsDisplayed
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .ScaleWithDocHeaderFooter = True
    .AlignMarginsHeaderFooter = True
End With


ActiveSheet.ExportAsFixedFormat pbFixedFormatPDF, "käyttöpäiväkirja.pdf"

End Sub

有什么想法吗?

【问题讨论】:

  • 原来问题与 Excel 无关。按下“X”后,Windows 8.1 PDF 阅读器并未完全关闭,这显然会阻止对文件进行更改。如果我从任务管理器中关闭阅读器,它工作正常。

标签: excel vba pdf


【解决方案1】:

我已经试过你的代码,它看起来很好,除了:

ActiveSheet.ExportAsFixedFormat pbFixedFormatPDF, "käyttöpäiväkirja.pdf"

您实际上需要提供文件的完整路径名以及 .pdf 扩展名。

我认为它类似于Drive:\Folder\SubFolder....\Filename.pdf。如果您要与其他用户共享它,您始终可以使用 Windows Special Folder 使用 FileSystemObject 类来获取与用户无关的路径。

我强烈建议您让 Excel 确定最后一行数据,而不是在您的 H 列中任意设置一个极高的 long。我认为这样做会让你的代码在运行时运行得更快:

Dim lastrow As Long
With ActiveSheet
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
End With
area = Range("B3:H" & lastrow).End(xlUp).SpecialCells(xlCellTypeConstants, 3).Address

【讨论】:

  • 我按照您的建议进行了更改。感谢您的帮助!
猜你喜欢
  • 2017-02-09
  • 2017-12-12
  • 1970-01-01
  • 2020-02-07
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-20
相关资源
最近更新 更多