Handling errors from a WinHttpReq function

most

Board Regular
Joined
Feb 22, 2011
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
Hi,
I would like some assistance with handling errors from this function.
SomeDownload1 will return WinHttpReq.Status = 404, what is the best practice of returning a error code back to sub routine?
SomeDownload2 will run-time error at line 6 (WinHttpReq.send), how to handle this?

I searched but could not find any match on my particular case.

VBA Code:
Function Download(myURL As String, LocalFilePath As String)
  Dim WinHttpReq As Object
  Dim oStream As Object
  Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
  WinHttpReq.Open "GET", myURL, False, "", "" '("username", "password")
  WinHttpReq.send

   If WinHttpReq.Status = 200 Then
      Set oStream = CreateObject("ADODB.Stream")
      oStream.Open
      oStream.Type = 1
      oStream.Write WinHttpReq.responseBody
      oStream.SaveToFile LocalFilePath, 2  '1=no overwrite, 2=overwrite
      oStream.Close
   End If

End Function
Sub SomeDownload1()
  Dim dl_URL As String
  Dim dl_FilePath As String
   dl_URL = "https://www.exist.com/doesnotexist.xlam"
   dl_FilePath = Application.UserLibraryPath & "\Test1.xlam"
  Call Download(dl_URL, dl_FilePath)
End Sub
Sub SomeDownload2()
  Dim dl_URL As String
  Dim dl_FilePath As String
   dl_URL = "https://www.doesnotexist.com/Whatever.xlam"
   dl_FilePath = Application.UserLibraryPath & "\Test2.xlam"
  Call Download(dl_URL, dl_FilePath)
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You can amend your Download function so that it returns True when successful, and False when it's unsuccessful. It can also be amended to return the error message to the calling procedure. Maybe something like this...

VBA Code:
Option Explicit

Sub test()

    Dim ErrorMessage As String
    Dim IsDownloadSuccessful As Boolean
    
    IsDownloadSuccessful = Download("http://xl-central.com/", "c:\users\domenic\desktop\sample.txt", ErrorMessage)
    
    If Not IsDownloadSuccessful Then
        MsgBox ErrorMessage, vbExclamation, "Error"
        Exit Sub
    End If
    
    'etc
    '
    '
    
End Sub

Function Download(myURL As String, LocalFilePath As String, ErrorMessage As String) As Boolean
    Dim WinHttpReq As Object
    Dim oStream As Object
    
    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    WinHttpReq.Open "GET", myURL, False, "", "" '("username", "password")
    WinHttpReq.Send
    
    With WinHttpReq
        If .Status <> 200 Then
            ErrorMessage = "Error " & .Status & ": " & .StatusText
            Download = False
            Exit Function
        End If
    End With

    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile LocalFilePath, 2  '1=no overwrite, 2=overwrite
    oStream.Close
    
    Download = True
End Function

Hope this helps!
 
Upvote 0
Your solution was really nice, but it stills ends with runtime error, i.e. if url would be "http://xl-entral.com/" (misspelled).
I just came up with this solution, not as nice as yours but it seems to work with my test cases.
I will investigate how I can combine best parts of our codes. Thanks!

VBA Code:
Function Download2(myURL As String, LocalFilePath As String)
  Dim WinHttpReq As Object
  Dim oStream As Object
  Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
  WinHttpReq.Open "GET", myURL, False, "", "" '("username", "password")
On Error Resume Next
  WinHttpReq.Send
On Error GoTo 0

   If WinHttpReq.Status = 200 Then
      Set oStream = CreateObject("ADODB.Stream")
      oStream.Open
      oStream.Type = 1
      oStream.Write WinHttpReq.ResponseBody
      oStream.SaveToFile LocalFilePath, 2  '1=no overwrite, 2=overwrite
      oStream.Close
     Else
      Download = WinHttpReq.Status
   End If

End Function
Sub SomeDownload1()
  Dim dl_URL As String
  Dim dl_FilePath As String
   dl_URL = "https://www.exist.com/doesnotexist.xlam"
   dl_FilePath = Application.UserLibraryPath & "\Test1.xlam"

  If Download(dl_URL, dl_FilePath) <> 0 Then
    Debug.Print "Error download"
   Else
    Call Download(dl_URL, dl_FilePath)
  End If

End Sub
Sub SomeDownload2()
  Dim dl_URL As String
  Dim dl_FilePath As String
   dl_URL = "https://www.doesnotexist.com/Whatever.xlam"
   dl_FilePath = Application.UserLibraryPath & "\Test2.xlam"
     
  If Download(dl_URL, dl_FilePath) <> 0 Then
    Debug.Print "Error download"
   Else
    Call Download(dl_URL, dl_FilePath)
  End If
 
End Sub
 
Upvote 0
In that case, how about amending it as follows...

VBA Code:
Function Download(myURL As String, LocalFilePath As String, ErrorMessage As String) As Boolean
    Dim WinHttpReq As Object
    Dim oStream As Object
    
    On Error GoTo ErrHandler
    
    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    WinHttpReq.Open "GET", myURL, False, "", "" '("username", "password")
    WinHttpReq.Send
    
    If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write WinHttpReq.ResponseBody
        oStream.SaveToFile LocalFilePath, 2  '1=no overwrite, 2=overwrite
        oStream.Close
        Download = True
        Exit Function
    End If
    
    With WinHttpReq
        ErrorMessage = "Error " & .Status & ": " & .StatusText
        Download = False
        Exit Function
    End With
    
ErrHandler:
    ErrorMessage = "Error " & Err.Number & ": " & Err.Description
    Download = False
    
End Function
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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