2017-10-04 138 views
0

我试图使用VBA将HTML另存为PDF。使用VBA将Wesite打印为PDF

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

以下是我的代码:我在Google上搜索某些内容,然后在第一个Google搜索页上输入链接,然后我想将搜索结果打印为PDF。

Sub Main() 

    Dim search As String 
    Dim save_path As String 
    Dim number As Integer 

    number = 5 
    save_path = "" 

    Dim query As String 
    query = InputBox("Enter here your search", "Google Search") 
    search = query 
    search = Replace(search, " ", "+") 
    PDFFolderSelection save_path, search 

    Debug.Print save_path, search 
    Google_Search search, save_path, number 
End Sub 

Sub Google_Search(search As String, save_path As String, number As Integer)  
    Dim IE As InternetExplorerMedium 
    Set IE = CreateObject("InternetExplorer.Application") 
    Dim HTMLDoc As MSHTML.HTMLDocument 

    IE.Navigate "http://google.com/#q=" & search 
    IE.Visible = True 

    Do While IE.ReadyState <> READYSTATE_COMPLETE 
    Loop 

    Set HTMLDoc = IE.Document 


    Dim RCS As MSHTML.IHTMLElementCollection 
    Dim RC As MSHTML.IHTMLElement 
    Set RCS = HTMLDoc.getElementsByClassName("rc") 
    Dim Atags As MSHTML.IHTMLElementCollection 
    Dim Atag As MSHTML.IHTMLElement 

    Dim URLs As New Collection 
    Dim URL As MSHTML.IHTMLElement 
    Set URLs = Nothing 


    For Each RC In RCS 
    Dim RS As MSHTML.IHTMLElementCollection 
    Dim R As MSHTML.IHTMLElement 
    Set RS = RC.getElementsByClassName("r") 
     For Each R In RS 
     Set Atags = R.getElementsByTagName("a") 
      For Each Atag In Atags 
      URLs.Add Atag 
      Next Atag 
     Next R 
    Next RC 

    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, "", "") 
     'Debug.Print str_text 
     IEs.Navigate str_text 
     IEs.Visible = True 
     Do While IEs.ReadyState <> READYSTATE_COMPLETE Or IEs.Busy 
     Loop 

     IEs.Quit 
     Set IEs = Nothing 
    Next URL 

    IE.Quit 
    Set IE = Nothing 

End Sub 
+0

使用谷歌找到了如何检查默认打印机,以及如何去改变它 – jsotola

+0

我试图找到解决方案,但我还没有找到任何这就是为什么我问在堆栈 – linke95

回答

0

这花了不到10分钟,发现

只是改变你的default printer窗口和检查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 
+0

感谢您的答案,但我怎么能打印网站到特定的文件夹。我有一个路径到文件夹。 – linke95

+0

这不是你原来的问题。请提交这个问题的另一个帖子。 – jsotola

0

我的代码看起来是这样的,现在我想打印网站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