首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PDFCreator通过VBA实现HTML到PDF的转换

用PDFCreator通过VBA实现HTML到PDF的转换
EN

Stack Overflow用户
提问于 2011-12-08 06:09:31
回答 1查看 10.8K关注 0票数 0

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

是否可以从IE中打开的HTML文件自动创建PDF?

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

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

EN

回答 1

Stack Overflow用户

发布于 2011-12-09 01:53:48

您可以自动执行IE,使其可以将文档打印到任何打印机,包括PDFCreator。

您可能还想检查this blog,它显示了如何让PDFCreator跳过“保存”对话框。我不是PowerShell方面的专家,所以我不会尝试将其转换为VBA语言

代码语言:javascript
复制
'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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8423300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档