2

我正在为自己制作一张 Excel 表格来跟踪我的投资。我有这个证券交易所网站的链接,该链接有一个 zip 文件,其中包含最后交易日交易数据的 CSV 文件。zip 文件的名称是动态的,格式为“eq_csv.zip,其中 ddmmyy 是数据所属的交易日的日期。因此,如果市场关闭。

我建立了一个模块,每次打开我的 excel 文件以获取最新的在线数据时在线检查。下面给出的代码应该从当前日期开始循环并向后移动 1 天,直到我下载了有效的 zip 文件。例如,如果当前日期是 4 月 28 日(星期日)并且在线资源上的文件是 4 月 26 日(星期五)(eq260413_CSV.zip),那么我的循环应该经过 3 次迭代(2 次没有文件消息和一个文件下载的味精)并下载文件 eq260413_CSV.zip。由于提到的在线链接中不存在文件 eq280413_CSV.zip 或 eq290413_CSV.zip,我预计会返回错误并继续循环。而在运行代码时,我发现该函数只是在第一次传递期间创建了一个没有数据的虚拟文件 eq280413_CSV.zip,并向 iRet 返回值 0,从而退出循环。

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()
4

1 回答 1

2

URLDownloadToFile文件/URL 不存在时,不应使用文件 API。

您必须首先检查 URL 是否有效,然后URLDownloadToFile在适用时使用。

使用 Leith Ross 编写的以下函数(取自这里

'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

当 URL 正常时,你会得到类似这样的东西

在此处输入图像描述

如果不是,你会得到这样的东西

在此处输入图像描述

所以你需要做的就是寻找200 - OK,如果你得到了,然后使用URLDownloadToFile下载文件。

于 2013-04-30T00:48:34.993 回答