HTTP - File Download

This enables you to download an internet file using the Microsoft XML object and is an alternative to using the URLDownloadToFile API.
Requires a reference to the Microsoft XML library
The responseBody method of the XMLHHTP object returns a byte array


Public Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean 
Dim oXMLHTTP As MSXML2.XMLHTTP
Dim i As Long,
Dim vFF As Long
Dim oResp() As Byte

    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    oXMLHTTP.Open "GET", vWebFile, False
    oXMLHTTP.Send
     
    Do While (oXMLHTTP.readyState <> 4 )
        DoEvents
    Loop
     
    oResp = oXMLHTTP.responseBody
     
    vFF = FreeFile
    If Dir(vLocalFile) <> "" Then
        Kill vLocalFile
    End If
    Open vLocalFile For Binary As #vFF
    Put #vFF, , oResp
    Close #vFF
     
    Set oXMLHTTP = Nothing
End Function
 
Public Sub Testing()
    Call SaveWebFile ("http://www.bettersolutions.com/websitelogo.png", "C:\websitelogo.png" )
    ActiveSheet.Pictures.Insert "C:\websitelogo.png"
    Kill "C:\C:\websitelogo.png"
End Sub


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext