Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用excel vba从web下载zip文件_Excel_Vba_Excel 2007 - Fatal编程技术网

使用excel vba从web下载zip文件

使用excel vba从web下载zip文件,excel,vba,excel-2007,Excel,Vba,Excel 2007,我是excel新手,下面使用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

我是excel新手,下面使用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
UrlFile=”https://www1.nseindia.com/content/historical/EQUITIES/2020/MAR/cm13MAR2020bhav.csv.zip"
转到退出时出错_
使用CreateObject(“MSXML2.XMLHTTP”)
.Open“GET”,UrlFile,False
.setRequestHeader“升级不安全请求”,“1”
.setRequestHeader“Sec Fetch Dest”、“document”
.发送
如果状态为200,则退出功能
b()=.responseBody
FN=自由文件
打开二进制访问写为#FN的路径名
Put#FN,b()
退出:
MsgBox错误说明
如果为FN,则关闭#FN
Url2File=.Status=200
以

在上述代码中执行.send时,它总是导致错误“指定资源的下载失败”。请帮助我解决此问题。

这是否回答了您的问题?我还使用了“Microsoft.XMLHTTP”,我的代码与给定链接中的代码类似。但我得到的错误下载失败。
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