Asynchronous File Downloads from Within VBA (Excel)

前端 未结 3 819
轮回少年
轮回少年 2020-12-15 00:30

I\'ve already tried using many different techniques with this... One that works pretty nicely but still ties up code when running is using the api call:

Priv         


        
3条回答
  •  孤城傲影
    2020-12-15 01:21

    You can do this using xmlhttp in asynchronous mode and a class to handle its events:

    http://www.dailydoseofexcel.com/archives/2006/10/09/async-xmlhttp-calls/

    The code there is addressing responseText, but you can adjust that to use .responseBody. Here's a (synchronous) example:

    Sub FetchFile(sURL As String, sPath)
     Dim oXHTTP As Object
     Dim oStream As Object
    
    
        Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
        Set oStream = CreateObject("ADODB.Stream")
        Application.StatusBar = "Fetching " & sURL & " as " & sPath
        oXHTTP.Open "GET", sURL, False
        oXHTTP.send
        With oStream
            .Type = 1 'adTypeBinary
            .Open
            .Write oXHTTP.responseBody
            .SaveToFile sPath, 2 'adSaveCreateOverWrite
            .Close
        End With
        Set oXHTTP = Nothing
        Set oStream = Nothing
        Application.StatusBar = False
    
    
    End Sub
    

提交回复
热议问题