使用 PDFCreator 通过 VBA 将 HTML 转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8423300/
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
HTML to PDF through VBA using PDFCreator
提问by user1028687
I have been trying to automate PDFCreator using VBA.
我一直在尝试使用 VBA 自动化 PDFCreator。
Can I automate the creation of PDF from HTML file opened in IE?
我可以从 IE 中打开的 HTML 文件自动创建 PDF 吗?
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.
我在网上的搜索给了我在 Excel 或 Word 中工作的代码,但我真正想要的是我将输入一个 HTML 文件路径到 VBA 表单,它应该打开,导航浏览器并将其打印为 PDF。
I know how to control PDFCreator by VBA, but I am not sure how do I link IE to the PDFCreator printer.
我知道如何通过 VBA 控制 PDFCreator,但我不确定如何将 IE 链接到 PDFCreator 打印机。
回答by GTG
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
您可以自动化 IE,让它将文档打印到任何打印机,包括 PDFCreator。
您可能还想查看此博客,它显示了如何让 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