Excel:导出为PDF不更新

时间:2015-02-28 12:50:36

标签: excel vba pdf

我试图让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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我已经尝试过你的代码了,除了:

之外看起来很好
ActiveSheet.ExportAsFixedFormat pbFixedFormatPDF, "käyttöpäiväkirja.pdf"

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

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

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

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