Excel 循环中的URLDownloadToFile函数不起作用,因为即使没有要下载的文件,该函数也返回0
我在为自己制作一张excel表格,以跟踪我的投资。我有证券交易所网站的这个链接,它有一个单一的zip文件,其中包含上一个交易日交易数据的CSV文件。zipfile的名称以“eq_csv.zip”的形式是动态的,其中ddmmyy是数据所属交易日的日期。因此,可用文件可能是当天的,如果市场关闭,则可能是2-4天的 我构建了一个模块,每次打开excel文件时都会进行在线检查,以获取最新的在线数据。下面给出的代码应该从当前日期开始循环,并向后移动1天,直到我下载了有效的zip文件。例如,如果当前日期为4月28日(星期日),在线资源上的文件是否为4月26日(星期五)(eq260413_CSV.zip),然后我的循环应该经过3次迭代(2次无文件msg和1次文件下载msg)并下载文件eq260413_CSV.zip。由于文件eq280413_CSV.zip或eq290413_CSV.zip在上述在线链接中不存在,我希望返回一个错误并继续循环。而在运行代码时,我发现该函数只创建了一个虚拟文件eq280413_CSV.zip,在第一次传递时没有数据,并返回一个0到的值我退出了循环。有人能帮忙吗Excel 循环中的URLDownloadToFile函数不起作用,因为即使没有要下载的文件,该函数也返回0,excel,download,vba,Excel,Download,Vba,我在为自己制作一张excel表格,以跟踪我的投资。我有证券交易所网站的这个链接,它有一个单一的zip文件,其中包含上一个交易日交易数据的CSV文件。zipfile的名称以“eq_csv.zip”的形式是动态的,其中ddmmyy是数据所属交易日的日期。因此,可用文件可能是当天的,如果市场关闭,则可能是2-4天的 我构建了一个模块,每次打开excel文件时都会进行在线检查,以获取最新的在线数据。下面给出的代码应该从当前日期开始循环,并向后移动1天,直到我下载了有效的zip文件。例如,如果当前日期为4
Sub DownloadFile()
Worksheets("Online Equity Data").Activate
Dim StrURL As String
Dim strPath As String
Dim dDate As Date
Dim iRet As Long
dDate = Now() + 1
iRet = 1
vFolderName = "C:\Users\Deep\Documents\Finances\Test\"
Do While iRet <> 0
dDate = dDate - 1
StrURL = "http://www.bseindia.com/download/BhavCopy/Equity/eq" & Format(dDate, "ddmmyy") & "_csv.zip"
strPath = vFolderName & "eq" & Format(dDate, "ddmmyy") & "_csv.zip"
iRet = URLDownloadToFile(0, StrURL, strPath, 0, 0)
If iRet= 0
MsgBox "File eq" & Format(dDate, "ddmmyy") & "_csv.zip Downloaded"
Else
MsgBox "No File Named eq" & Format(dDate, "ddmmyy") & "_csv.zip"
End If
Loop
'More code Here to unzip and import the downloaded data
End Sub()
子下载文件()
工作表(“在线股权数据”)。激活
暗弦
将strPath设置为字符串
日期
暗淡的头发
dDate=Now()+1
iRet=1
vFolderName=“C:\Users\Deep\Documents\financials\Test\”
当iRet 0时执行此操作
dDate=dDate-1
StrURL=”http://www.bseindia.com/download/BhavCopy/Equity/eq&格式(dDate,“ddmmyy”)和“_csv.zip”
strPath=vFolderName&“eq”和格式(dDate,“ddmmyy”)和“_csv.zip”
iRet=URL下载文件(0,StrURL,strPath,0,0)
如果iRet=0
MsgBox“文件eq”和格式(dDate,“ddmmyy”)和“_csv.zip下载”
其他的
MsgBox“没有名为eq的文件”和格式(dDate,“ddmmyy”)和“_csv.zip”
如果结束
环
'此处有更多代码用于解压缩和导入下载的数据
结束分段()
当文件/URL不存在时,不应使用URLDownloadToFile
文件API
您必须首先检查URL是否有效,然后使用URLDownloadToFile
(如果适用)
使用Leith Ross编写的以下函数(摘自)
”写于2011年3月15日
作者:莱思·罗斯
作为字符串的公共页面源
作为对象的公共httpRequest
函数GetURLStatus(ByVal URL作为字符串,可选AllowerStatus作为布尔值)
Const WinHttpRequestOption_UserAgentString=0
Const WinHttpRequestOption_EnableRedirects=6
出错时继续下一步
设置httpRequest=CreateObject(“WinHttp.WinHttpRequest.5.1”)
如果httpRequest什么都不是,那么
设置httpRequest=CreateObject(“WinHttp.WinHttpRequest.5”)
如果结束
呃,明白了
错误转到0
httpRequest.Option(WinHttpRequestOption_UserAgentString)=“Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.0)”
httpRequest.Option(WinHttpRequestOption_EnableRedirects)=AllowRedirects
'清除任何以前的网页源信息
PageSource=“”
'如果缺少,请添加协议
如果InStr(1,URL,“:/”)=0,则
URL=“http://”&URL
如果结束
'同步启动HTTP httpRequest
出错时继续下一步
httpRequest.Open“GET”,URL,False
如果错误号为0,则
'处理连接错误
GetURLStatus=Err.Description
呃,明白了
退出功能
如果结束
错误转到0
'发送http httpRequest以获取服务器状态
出错时继续下一步
httpRequest.Send
httpRequest.WaitForResponse
如果错误号为0,则
'处理服务器错误
PageSource=“错误”
GetURLStatus=Err.Description
呃,明白了
其他的
'显示HTTP响应信息
GetURLStatus=httpRequest.Status&“-”&httpRequest.StatusText
'保存网页文本
PageSource=httpRequest.responsetext
如果结束
错误转到0
端函数
当URL是确定的,你会得到这样的东西
如果不是,你会得到这样的结果
所以你需要做的就是查找
200-好的
,如果你找到了,然后使用URLDownloadToFile
下载该文件。交叉发布在抱歉的交叉发布中……我误解了这个词的意思是在一个网站内。我最初也在[excel论坛]()有一天,excel论坛没有解决方案,我自己的搜索也没有到达任何地方,这导致我尝试了不同的网站。我猜是VBA仙境的新手的不耐烦和沮丧。但教训很好。在这种情况下,我建议在那里提及此链接。否则该网站的许多专家将浪费他们的时间我试图解决它。:)谢谢你的诚实:)我已经做到了…谢谢你@Siddharth Rout…像做梦一样工作,使用了Lieth Ross的函数并将我的代码修改为以下内容。`Do While iRet 200 dDate=dDate-1 StrURL=“MyURL/eq”&Format(dDate,“ddmmyy”)&“_csv.zip”iRet=Val(GetURLStatus(StrURL,False))循环`
'Written: March 15, 2011
'Author: Leith Ross
Public PageSource As String
Public httpRequest As Object
Function GetURLStatus(ByVal URL As String, Optional AllowRedirects As Boolean)
Const WinHttpRequestOption_UserAgentString = 0
Const WinHttpRequestOption_EnableRedirects = 6
On Error Resume Next
Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
If httpRequest Is Nothing Then
Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5")
End If
Err.Clear
On Error GoTo 0
httpRequest.Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
httpRequest.Option(WinHttpRequestOption_EnableRedirects) = AllowRedirects
'Clear any pervious web page source information
PageSource = ""
'Add protocol if missing
If InStr(1, URL, "://") = 0 Then
URL = "http://" & URL
End If
'Launch the HTTP httpRequest synchronously
On Error Resume Next
httpRequest.Open "GET", URL, False
If Err.Number <> 0 Then
'Handle connection errors
GetURLStatus = Err.Description
Err.Clear
Exit Function
End If
On Error GoTo 0
'Send the http httpRequest for server status
On Error Resume Next
httpRequest.Send
httpRequest.WaitForResponse
If Err.Number <> 0 Then
' Handle server errors
PageSource = "Error"
GetURLStatus = Err.Description
Err.Clear
Else
'Show HTTP response info
GetURLStatus = httpRequest.Status & " - " & httpRequest.StatusText
'Save the web page text
PageSource = httpRequest.responsetext
End If
On Error GoTo 0
End Function