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 i As Long,
Dim vFF As Long
Dim oResp() As Byte

    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    oXMLHTTP.Open "GET", vWebFile, False
    Do While (oXMLHTTP.readyState <> 4 )
    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 ("", "C:\websitelogo.png" )
    ActiveSheet.Pictures.Insert "C:\websitelogo.png"
    Kill "C:\C:\websitelogo.png"
End Sub

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