vba 如何在 Excel VB 宏中发布为 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7487836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:04:34  来源:igfitidea点击:

How to publish to PDF in Excel VB macro?

excelvba

提问by TruMan1

I have a worksheet with a macro that generates a web page (code is below). I would like to change this so it generates a PDF instead. Can this be done?

我有一个带有生成网页的宏的工作表(代码如下)。我想改变它,以便它生成一个 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:$q", xlHtmlStatic, "custweb08 current_8162", "").Publish (True)


    Next x

End Sub

采纳答案by Bek Raupov

If you are using Excel 2010>, it allows you to save your workbook/excel sheet as PDF as built in functionality, we used to have report specifically run in Excel just because of that :)

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

You can try to record while it trying to "Save As..." as PDF and it might look like this:

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

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

Note: I have tried it on Excel 2010, it might look different in other versions.

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