most
Board Regular
- Joined
- Feb 22, 2011
- Messages
- 107
- Office Version
- 365
- 2019
- Platform
- Windows
- 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.
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