使用VBA将Wesite打印为PDF

使用VBA将Wesite打印为PDF,vba,pdf,printing,Vba,Pdf,Printing,我正在尝试使用VBA将HTML保存为PDF 我不知道如何检查默认打印机,将其更改为Microsoft Print to PDF,然后再返回到旧打印机 下面是我的代码:我在谷歌上搜索一些东西,然后在第一个谷歌搜索页面上输入链接,然后我想将搜索结果打印成PDF Sub-Main() 以字符串形式搜索 将路径另存为字符串 整数形式的数字 数字=5 save_path=“” 将查询设置为字符串 query=InputBox(“在此输入您的搜索”、“谷歌搜索”) 搜索=查询 搜索=替换(搜索“,”+”)

我正在尝试使用VBA将HTML保存为PDF

我不知道如何检查默认打印机,将其更改为Microsoft Print to PDF,然后再返回到旧打印机

下面是我的代码:我在谷歌上搜索一些东西,然后在第一个谷歌搜索页面上输入链接,然后我想将搜索结果打印成PDF

Sub-Main()
以字符串形式搜索
将路径另存为字符串
整数形式的数字
数字=5
save_path=“”
将查询设置为字符串
query=InputBox(“在此输入您的搜索”、“谷歌搜索”)
搜索=查询
搜索=替换(搜索“,”+”)
PDF文件夹选择保存路径,搜索
调试。打印保存路径,搜索
谷歌搜索,保存路径,号码
端接头
子谷歌搜索(搜索为字符串,保存为字符串,数字为整数)
Dim IE作为InternetExplorerMedium
设置IE=CreateObject(“InternetExplorer.Application”)
将HTMLDoc设置为MSHTML.HTMLDocument
即“导航”http://google.com/#q=“&搜索
可见=真实
在IE.ReadyState ReadyState\u完成时执行此操作
环
设置HTMLDoc=IE.Document
将RCS设置为MSHTML.IHTMLElementCollection
将RC标注为MSHTML.IHTMLElement
设置RCS=HTMLDoc.getElementsByClassName(“rc”)
Dim Atags作为MSHTML.IHTMLElementCollection
Dim Atag作为MSHTML.IHTMLElement
将URL设置为新集合
将URL设置为MSHTML.IHTMLElement
设置URL=Nothing
对于RCS中的每个RC
将RS设置为MSHTML.IHTMLElementCollection
将R作为MSHTML.IHTMLElement进行调整
设置RS=RC.getElementsByClassName(“r”)
对于RS中的每个R
Set Atags=R.getElementsByTagName(“a”)
对于Atag中的每个Atag
URL.addatag
下一个Atag
下一个R
下一个RC
对于URL中的每个URL
作为InternetExplorerMedium的dime
设置IEs=CreateObject(“InternetExplorer.Application”)
str_text=Replace(URL.getAttribute(“href”)、“”、“”)
str_text=Replace(str_text,“,”)
'Debug.Print str_text
导航str_文本
可见=真
当IEs.ReadyState ReadyState_完成或IEs.Busy时执行
环
不,退出
设置IEs=无
下一个URL
即退出
设置IE=无
端接头

这花了不到10分钟的时间找到

只需在windows中更改您的
默认打印机
,并检查
Application.ActivePrinter
的值,即可获得您要使用的打印机的确切名称

有几种方法可以通过执行系统调用来获取系统打印机列表

Option Explicit

Sub switchPrinters()
    Dim ptr As String

    ptr = Application.ActivePrinter
    Application.ActivePrinter = "HP Deskjet 3520 USB on Ne03:" ' "printer_name on port_name" 
    Activesheet.Printout
    Application.ActivePrinter = ptr

End Sub

我的代码看起来像这样,现在我想把网站打印成PDF格式

Dim sPrinter As String
Dim sDefaultPrinter As String
Debug.Print "Default printer: ", Application.ActivePrinter
sDefaultPrinter = Application.ActivePrinter ' store default printer
sPrinter = GetPrinterFullName("Microsoft Print to PDF")
If sPrinter = vbNullString Then ' no match
    Debug.Print "No match"
Else
    Application.ActivePrinter = sPrinter
    Debug.Print "Temp printer: ", Application.ActivePrinter
    ' do something with the temp printer
    ' Przechodzenie przez strony wraz z zapisem
    For Each URL In URLs
     Dim IEs As InternetExplorerMedium
     Set IEs = CreateObject("InternetExplorer.Application")
     str_text = Replace(URL.getAttribute("href"), "", "")
     str_text = Replace(str_text, "", "")

     IEs.Navigate str_text
     IEs.Visible = True
     Do While IEs.ReadyState <> READYSTATE_COMPLETE Or IEs.Busy
     Loop



     'HERE I WOLULD LIKE TO PRINT IEs TO PDF (specific path, and filename:)


     IEs.Quit
     Set IEs = Nothing
    i = i + 1
    If i = 5 Then Exit For
    Next URL
    Application.ActivePrinter = sDefaultPrinter ' restore default printer
End If
Debug.Print "Default printer: ", Application.ActivePrinter
Dim sPrinter作为字符串
将打印机设置为字符串
调试。打印“默认打印机:”,Application.ActivePrinter
sDefaultPrinter=Application.ActivePrinter的存储默认打印机
sPrinter=GetPrinterFullName(“Microsoft打印到PDF”)
如果sPrinter=vbNullString,则“不匹配”
调试。打印“不匹配”
其他的
Application.ActivePrinter=sprint
调试。打印“临时打印机:”,Application.ActivePrinter
“用临时打印机做些什么
“Przechodzenie przez strony wraz zapisem
对于URL中的每个URL
作为InternetExplorerMedium的dime
设置IEs=CreateObject(“InternetExplorer.Application”)
str_text=Replace(URL.getAttribute(“href”)、“”、“”)
str_text=Replace(str_text,“,”)
导航str_文本
可见=真
当IEs.ReadyState ReadyState_完成或IEs.Busy时执行
环
'在这里,我想将IEs打印成PDF格式(具体路径和文件名:)
不,退出
设置IEs=无
i=i+1
如果i=5,则退出以进行测试
下一个URL
Application.ActivePrinter=sDefaultPrinter还原默认打印机
如果结束
调试。打印“默认打印机:”,Application.ActivePrinter

使用谷歌查找如何检查默认打印机以及如何更改打印机我试图找到解决方案,但没有找到任何解决方案,这就是我在Stack上提问的原因谢谢你的回答,但我如何将网站打印到特定文件夹。我有一个文件夹的路径。这不是你最初的问题。请为这个问题提交另一个帖子。