如何在Excel VBA宏中发布到PDF?

时间:2011-09-20 15:37:34

标签: excel vba

我有一个带有生成网页的宏的工作表(代码如下)。我想改变它,所以它生成一个PDF。可以这样做吗?

Sub publishwebpages()

'calculate how many iterations
    x = 0
    Sheets("webpagegen").Select
    Range("n1").Select
    Range("n1").Copy
    noofpages = ActiveCell.Value


'step through and select each customer

    For x = 0 To noofpages
    'For x = 364 To 366

    Sheets("listcust").Select
    Range("A2").Select
    ActiveCell.Offset(x, 0).Range("A1").Select
    Selection.Copy
    Sheets("webpagegen").Select
    Range("l1").Select
    ActiveSheet.Paste



'name folder and filename
    Sheets("webpagegen").Select
    Range("ac2").Select
    Range("ac2").Copy
    foldername = ActiveCell.Value


'publish pages
    Range("d3:q80").Select
    Application.CutCopyMode = False
    Selection.Copy
    ActiveWorkbook.PublishObjects.Add(xlSourceRange, "D:\Temp\" & foldername, "webpagegen", "$d$3:$q$80", xlHtmlStatic, "custweb08 current_8162", "").Publish (True)


    Next x

End Sub

1 个答案:

答案 0 :(得分:2)

如果您使用的是Excel 2010>,它允许您将工作簿/ Excel工作表保存为PDF作为内置功能,我们曾经因为这个而在Excel中专门运行报告:)

您可以尝试在尝试将“另存为...”作为PDF时进行录制,它可能如下所示:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "c:\Book1.pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

注意:我在Excel 2010上尝试过它,在其他版本中看起来可能有所不同。