2

私はVBAの初心者です。http://cetatenie.just.ro/wp-content/uploads/からUrlDownloadToFileを使用してPDFファイルをダウンロードするにはどうすればよいですか?

誰かがこれを手伝ってくれる?このコードは、ハイパーリンクを使用して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
4

1 に答える 1

2

申し訳ありませんが、URLDownloadToFileはAPI呼び出しであり、VBAのSQL "%"に相当する質問全体に答えることができたはずです。

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

次に、サンプルのその1行を次のように変更します

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

その後、あなたは行ってもいいはずです。別のファイル名が必要な場合は、反復ごとに変更するロジックをコーディングする必要があります。

于 2013-03-26T18:36:42.340 に答える