【问题标题】:Download zip file from web using excel vba使用 excel vba 从 Web 下载 zip 文件
【发布时间】:2020-07-19 12:45:38
【问题描述】:

我是 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,总是报错“指定资源下载失败”。请帮我解决这个问题。 p>

【问题讨论】:

标签: excel vba excel-2007


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2018-02-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    相关资源
    最近更新 更多