从Web下载文件(PDF) [英] Download Files (PDF) from Web
问题描述
如何使用UrlDownloadToFile从 http://cetatenie.just.ro下载PDF文件/ wp-content / uploads / ?
有人可以帮忙吗?该代码正在搜索PDF文件udner超链接,并在一些标准下匹配它们,即他们名下的当年。
功能UrlDownloadToFile (lNum As Long,sUrl As String,sPath As String,_
lNum1 As Long,lNum2 As Long)As Long
UrlDownloadToFile = 0
End Function
Sub DownPDF ()
'此宏从网页下载pdf文件
'需要下载MSXML2和MSHTML解析器并安装
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/
'获取目录l isting
设置xHttp =新的MSXML2.XMLHTTP
xHttp.OpenGET,sUrl
xHttp.send
'等待页面加载
直到xHttp.readyState = 4
DoEvents
循环
'将页面放在HTML文档中
设置hDoc =新的MSHTML.HTMLDocument
hDoc。 body.innerHTML = xHttp.responseText
'循环通过目录列表上的超链接
对于i = 0到hDoc.getElementsByTagName(a)。Length - 1
设置hAnchor = hDoc.getElementsByTagName(a)。Item(i)
'测试路径名以查看它是否匹配您的模式
如果hAnchor.pathname像Ordin - *。2013。 pdf然后
Ret = UrlDownloadToFile(0,sUrl& hAnchor.pathname,sPath,0,0)
如果Ret = 0然后
Debug.Print sUrl& hAnchor.pathname& 下载到& sPath
Else
Debug.Print sUrl& hAnchor.pathname& 没有下载
如果
结束If If
Next i
End Sub
对不起 - 我应该猜到URLDownloadToFile是一个API调用,可以在 SQL%相当于VBA 。
完全删除名为URLDownloadToFile的函数。将此粘贴到模块的顶部,您的样本过程
私有声明函数URLDownloadToFile Liburlmon别名URLDownloadToFileA_
(ByVal pCaller As Long,_
ByVal szURL As String,_
ByVal szFileName As String,_
ByVal dwReserved As Long,_
ByVal lpfnCB As Long) As Long
现在更改样本中的一行看起来像这样
Ret = URLDownloadToFile(0,sUrl& hAnchor.pathname,sPath& hAnchor.pathname,0,0)
那么你应该很好去。如果你想要一些不同的文件名,那么你必须编写一些逻辑来在每次迭代中改变它。
I am fresh-starter at VBA. How do I download PDF file using UrlDownloadToFile from http://cetatenie.just.ro/wp-content/uploads/?
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.
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
Sorry - I should have guessed that URLDownloadToFile was an API call and could have answered the whole question at SQL "%" equivalent in VBA.
Remove the function named URLDownloadToFile completely. Paste this at the top of the module where your Sample procedure is
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
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.
这篇关于从Web下载文件(PDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!