如何使用Excel宏将多个网页另存为pdf文件或屏幕截图?

时间:2018-09-18 05:06:46

标签: html excel excel-vba google-chrome web-scraping

我尝试了下面的代码,但是它仅保存页面的HTML内容,并且最终排除了页面上的大多数重要内容。

如果有人可以帮助我将这些网页另存为pdf或png文件,那就太好了。

Sub SaveWebpage()
    Dim objHTTP As Object, _
        objFSO As Object, _
        objFil As Object, _
        lngRow As Long, _
        strURL As String, _
        strPath As String, _
        strFilename As String
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'On the next line change the file path as desired.  Make sure the path ends with \
    strPath = "C:\Users\Pradyumna\Desktop\Save Webpages"
    For lngRow = 2 To Application.ActiveSheet.UsedRange.Rows.Count
        strFilename = strPath & lngRow & ".htm"
        strURL = Application.ActiveSheet.Cells(lngRow, 1).Value
        objHTTP.Open "GET", strURL, False
        objHTTP.send ""
        Set objFil = objFSO.CreateTextFile(strFilename)
        objFil.Write objHTTP.responseText
        objFil.Close
    Next
    Set objHTTP = Nothing
    Set objFSO = Nothing
    Set objFil = Nothing
    MsgBox "All done!"
End Sub

1 个答案:

答案 0 :(得分:0)

您可以通过命令行using headless Chrome将网页另存为PDF:

lRet = CreateObject("WScript.Shell").Run("""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" --headless --disable-gpu --print-to-pdf=""C:\Test\Sample.pdf"" https://api.myip.com", 1, True)
MsgBox "Ret code: " & lRet

您需要指定Chrome以及要保存的PDF文件的实际路径。请注意,您需要使用Chrome版本60或更高版本才能在Windows上运行代码。