使用 excel vba 从网络下载 zip 文件

Download zip file from web using excel vba

我是 excel 的新手,我正在关注 this link 使用 excel vba.

下载 zip 文件
        UrlFile = "https://www1.nseindia.com/content/historical/EQUITIES/2020/MAR/cm13MAR2020bhav.csv.zip"
        On Error GoTo exit_
        With CreateObject("MSXML2.XMLHTTP")
         .Open "GET", UrlFile, False
         .setRequestHeader "Upgrade-Insecure-Requests", "1"
         .setRequestHeader "Sec-Fetch-Dest", "document"
         .send
        If .Status <> 200 Then Exit Function
        b() = .responseBody
        FN = FreeFile
        Open PathName For Binary Access Write As #FN
        Put #FN, , b()
    exit_:
         MsgBox Err.Description
        If FN Then Close #FN
        Url2File = .Status = 200
      End With

在上面的代码中执行 .send 时,它总是导致错误 "The download of specified resource has failed" 。请帮助我解决这个问题问题。

Public Function DownloadFile()

Dim myURL As String
myURL = "https://www1.nseindia.com/content/historical/EQUITIES/2020/MAR/cm13MAR2020bhav.csv.zip"

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.send

If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    oStream.SaveToFile "C:\Users\praburaj\Downloads\file.zip", 2 ' 1 = no overwrite, 2 = overwrite
    oStream.Close
End If

End Function