簡體   English   中英

使用 VBA 將 Wesite 打印為 PDF

[英]Print Wesite to PDF using VBA

我正在嘗試使用 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

我的代碼看起來像這樣,現在我想將網站打印為 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

這花了不到 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM