vba 从 Web 下载文件 (PDF)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15641717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:16:25  来源:igfitidea点击:

Download Files (PDF) from Web

excelvbaexcel-vbadownload

提问by maximladus

I am fresh-starter at VBA. How do I download PDF file using UrlDownloadToFile from http://cetatenie.just.ro/wp-content/uploads/?

我是 VBA 的新手。如何使用 UrlDownloadToFile 从http://cetatenie.just.ro/wp-content/uploads/下载 PDF 文件?

Can anybody help with this? The code is searching the PDF files udner hyperlinks and matches them under some criteria, i.e. the current year under their name.

有人可以帮忙吗?该代码正在搜索 PDF 文件和超链接,并在某些条件下匹配它们,即它们名称下的当前年份。

Function UrlDownloadToFile(lNum As Long, sUrl As String, sPath As String, _
                           lNum1 As Long, lNum2 As Long) As Long

    UrlDownloadToFile = 0

    End Function


    Sub DownPDF()
    ' This macro downloads the pdf file from webpage
    ' Need to download MSXML2 and MSHTML parsers and install

    Dim sUrl As String
    Dim xHttp As MSXML2.XMLHTTP
    Dim hDoc As MSHTML.HTMLDocument
    Dim hAnchor As MSHTML.HTMLAnchorElement
    Dim Ret As Long
    Dim sPath As String
    Dim i As Long

    sPath = "C:\Documents and Settings\ee28118\Desktop\"
    sUrl = "http://cetatenie.just.ro/wp-content/uploads/"

    'Get the directory listing
    Set xHttp = New MSXML2.XMLHTTP
    xHttp.Open "GET", sUrl
    xHttp.send

    'Wait for the page to load
    Do Until xHttp.readyState = 4
        DoEvents
    Loop

    'Put the page in an HTML document
    Set hDoc = New MSHTML.HTMLDocument
    hDoc.body.innerHTML = xHttp.responseText

    'Loop through the hyperlinks on the directory listing
    For i = 0 To hDoc.getElementsByTagName("a").Length - 1
        Set hAnchor = hDoc.getElementsByTagName("a").Item(i)

        'test the pathname to see if it matches your pattern
        If hAnchor.pathname Like "Ordin-*.2013.pdf" Then
            Ret = UrlDownloadToFile(0, sUrl & hAnchor.pathname, sPath, 0, 0)

            If Ret = 0 Then
                Debug.Print sUrl & hAnchor.pathname & " downloaded to " & sPath
            Else
                Debug.Print sUrl & hAnchor.pathname & " not downloaded"
            End If
        End If
    Next i

    End Sub

采纳答案by Dick Kusleika

Sorry - I should have guessed that URLDownloadToFile was an API call and could have answered the whole question at SQL "%" equivalent in VBA.

抱歉 - 我应该猜到 URLDownloadToFile 是一个 API 调用,并且可以在 VBA 中的 SQL "%" 等效处回答整个问题。

Remove the function named URLDownloadToFile completely. Paste this at the top of the module where your Sample procedure is

完全删除名为 URLDownloadToFile 的函数。将此粘贴到您的示例程序所在的模块顶部

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
    (ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) As Long

Now change that one line in Sample to look like this

现在将 Sample 中的这一行更改为如下所示

Ret = URLDownloadToFile(0, sUrl & hAnchor.pathname, sPath & hAnchor.pathname, 0, 0)

Then you should be good to go. If you want some different file name, then you'll have to code some logic to change it at each iteration.

那么你应该很高兴去。如果您想要一些不同的文件名,那么您必须编写一些逻辑来在每次迭代时更改它。