通过使用PDFCreator的VBA将HTML转换为PDF [英] HTML to PDF through VBA using PDFCreator

查看:755
本文介绍了通过使用PDFCreator的VBA将HTML转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



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



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


您可以自动将IE打印到任何打印机上,包括PDFCreator。

您可能还想检查这个博客,它显示了如何让PDFCreator跳过保存对话框。我不是PowerShell的专家,所以我不会尝试将其转换为VBA

 '使用IE的函数将Google.com的内容打印为PDF文档
Sub printgoogle()
Dim Explorer As Object
Dim eQuery As Long'QueryStatusWB返回值类型
Dim i As Integer
Dim fTime As Single

'请参阅下面的函数,将默认打印机设置为PDFCreator。注意:如果您检查了什么是当前的默认打印机并在完成打印后将其设置回来,用户可能会很感激
SetDefaultPrinterPDFCreator

'连接到Internet Explorer
设置资源管理器= CreateObject(InternetExplorer.Application)
'打开一些文档。这通常是您计算机上的文件,但我在此处使用Google进行测试
Explorer.Navigatewww.google.com

TryAgain:
'等待2秒钟让IE加载文档
fTime = Timer
Do当fTime>定时器 - 2
DoEvents
循环
eQuery = Explorer.QueryStatusWB(6)'get print command status
如果eQuery和2然后
Explorer.ExecWB 6,2, ,'确定打印?然后执行Print(6)命令,而不显示打印对话框(2)
'等待2秒钟,同时IE打印
fTime = Timer
Do当fTime> Timer - 2
DoEvents
Loop
Else
GoTo TryAgain
End If

End Sub

'This函数将Windows默认打印机设置为您插入的任何打印机作为参数
Public Sub SetDefaultPrinter(ByVal printerName As String)
Dim oSH As WshNetwork
Set oSH = New WshNetwork
oSH.SetDefaultPrinter printerName
Set oSH = Nothing
End Sub


I have been trying to automate PDFCreator using VBA.

Can I automate the creation of PDF from HTML file opened in IE?

My search on the web gave me codes that work within Excel or Word but what I really want is that I will input a HTML file path to a VBA form and it should open, navigate the browser and print it to PDF.

I know how to control PDFCreator by VBA, but I am not sure how do I link IE to the PDFCreator printer.

解决方案

You can automate IE to let it print documents to any printer, including PDFCreator.
You may also want to check this blog, it shows how to let PDFCreator skip the "save" dialog. I'm not an expert on PowerShell, so I won't try to convert this to 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

这篇关于通过使用PDFCreator的VBA将HTML转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆