【问题标题】:HTML to PDF through VBA using PDFCreator使用 PDFCreator 通过 VBA 将 HTML 转为 PDF
【发布时间】:2012-01-15 10:27:18
【问题描述】:

我一直在尝试使用 VBA 自动化 PDFCreator。

我可以从在 IE 中打开的 HTML 文件自动创建 PDF 吗?

我在网络上的搜索给了我在 Excel 或 Word 中工作的代码,但我真正想要的是我将输入一个 HTML 文件路径到 VBA 表单,它应该打开,浏览浏览器并将其打印为 PDF。

我知道如何通过 VBA 控制 PDFCreator,但我不确定如何将 IE 链接到 PDFCreator 打印机。

【问题讨论】:

    标签: html vba pdf


    【解决方案1】:

    您可以自动化 IE,让它将文档打印到任何打印机,包括 PDFCreator。
    您可能还想查看this blog,它显示了如何让 PDFCreator 跳过“保存”对话框。我不是 PowerShell 方面的专家,所以我不会尝试将其转换为 VBA

    'A function that uses IE to print the contents of Google.com to a PDF document
    Sub printgoogle()
        Dim Explorer As Object
        Dim eQuery As Long 'return value type for QueryStatusWB
        Dim i As Integer
        Dim fTime As Single
    
        'See function below, to set the default printer to PDFCreator.  Note:  The user would probably be grateful if you checked to see what is the current default printer and set it back when finished printing
        SetDefaultPrinter "PDFCreator"
    
        'Connect to Internet Explorer
        Set Explorer = CreateObject("InternetExplorer.Application")
        'Open some document.  This is usually a file on your computer, but I use Google here for test purposes
        Explorer.Navigate "www.google.com"
    
    TryAgain:
            'Wait for 2 seconds to let IE load the document
            fTime = Timer
            Do While fTime > Timer - 2
                DoEvents
            Loop
            eQuery = Explorer.QueryStatusWB(6)  'get print command status
            If eQuery And 2 Then
                Explorer.ExecWB 6, 2, "", ""   'Ok to Print? Then execute the Print (6) command, without displaying the print dialog (2)
                'Wait for 2 seconds while IE prints
                fTime = Timer
                Do While fTime > Timer - 2
                    DoEvents
                Loop
            Else
                GoTo TryAgain
            End If
    
    End Sub
    
    'This function sets the Windows default printer to whatever printer you insert as parameter
    Public Sub SetDefaultPrinter(ByVal printerName As String)
        Dim oSH As WshNetwork
        Set oSH = New WshNetwork
        oSH.SetDefaultPrinter printerName
        Set oSH = Nothing
    End Sub
    

    【讨论】:

    • 我们如何设置路径以便将 PDF 保存在那里?
    猜你喜欢
    • 2020-06-11
    • 2012-02-07
    • 2012-04-12
    • 2015-08-18
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    相关资源
    最近更新 更多