Return values for SaveWebFile

KenCriss

Active Member
Joined
Jun 6, 2005
Messages
326
Found this piece of code which works well for downloading files from the web but am wondering if anyone know the return values...

Code:
Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean

Dim oXMLHTTP As Object, i As Long, vFF As Long, oResp() As Byte

Set oXMLHTTP = CreateObject("msxml2.xmlhttp")

oXMLHTTP.Open "GET", vWebFile, False
oXMLHTTP.send
oResp = oXMLHTTP.ResponseBody

vFF = FreeFile
If Dir(vLocalFile) <> "" Then Kill vLocalFile
Open vLocalFile For Binary As #vFF
Put #vFF, , oResp
Close #vFF
Set oXMLHTTP = Nothing
End Function

I have tried calling it like this
Code:
If (SaveWebFile "https://xxxxxxx.com/whatever.zip", ThePath & "\PUpdate.zip") = True
...but if it fails, it does not necessarily come back with a False like I thought it would.

Any thoughts?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Although declared as 'function returning Boolean', the return value of SaveWebFile is never defined so it always returns False.

You could change it to check the XMLHttp.statusText property and return True or False, like this:
Code:
Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean

    Dim oXMLHTTP As Object, i As Long, vFF As Long, oResp() As Byte
    
    Set oXMLHTTP = CreateObject("msxml2.xmlhttp")
    
    oXMLHTTP.Open "GET", vWebFile, False
    oXMLHTTP.send
    oResp = oXMLHTTP.ResponseBody
    
    Debug.Print oXMLHTTP.Status, oXMLHTTP.statusText
    
    If oXMLHTTP.statusText = "OK" Then
        vFF = FreeFile
        If Dir(vLocalFile) <> "" Then Kill vLocalFile
        Open vLocalFile For Binary As #vFF
        Put #vFF, , oResp
        Close #vFF
        SaveWebFile = True
    Else
        SaveWebFile = False
    End If
    
    Set oXMLHTTP = Nothing
    
End Function
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top