【问题标题】:Print Wesite to PDF using VBA使用 VBA 将网站打印为 PDF
【发布时间】:2018-03-15 12:07:09
【问题描述】:

我正在尝试使用 VBA 将 HTML 保存为 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

【问题讨论】:

  • 使用谷歌了解如何检查默认打印机以及如何更改它
  • 我试图找到解决方案,但我没有找到任何这就是我在堆栈上提问的原因

标签: vba pdf printing


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    不到 10 分钟就找到了

    只需在 Windows 中更改您的 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
    

    【讨论】:

    • 感谢您的回答,但我如何将网站打印到特定文件夹。我有文件夹的路径。
    • 这不是您最初的问题。请为此问题提交另一篇文章。
    猜你喜欢
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2017-04-05
    • 2011-12-28
    • 2014-12-05
    • 2015-08-05
    相关资源
    最近更新 更多