Web query error

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
754
Sometimes I get an error when getting data from the web. (Either due to lost internet connection or slow internet connections.) The error occurs on Send. if, for example, the website can not be reached, lost internet, or slow internet, I would like to give a message to each scenario. How can I fix this?

Code:
    With Request
        .Open "GET", URL, False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
[B]        .send[/B]
        .waitForResponse
        Response = .responseText
    End With



Thank you kindly.
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
349
Before your .send use On Error goto errorlabel. Place the label at the bottom like Reach:
and then do your error handling an create message boxes if needed.

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/goto-statement

Something LIKE this
For website could not be reached and no internet you could send a request to a website like google.com and store the .status. If they are both “OK” (check what it should be In the locals window just in case) then the user has internet and the website could be reached.
 
Last edited:

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
754
Thank you.. I used the error handling, but I'll look into the other part. Thanks. Cheers
 

MoshiM

Active Member
Joined
Jan 31, 2018
Messages
349
Thank you.. I used the error handling, but I'll look into the other part. Thanks. Cheers
This may help
Code:
Function URL_Exists(URL As String) As Boolean
    Dim Request As Object
    Dim ff As Integer
    Dim rc As Variant
    
    On Error GoTo EndNow
    Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
    With Request
      .Open "GET", URL, False
      .send
      rc = .StatusText
    End With
    Set Request = Nothing
    If rc = "OK" Then
        URL_Exists = True
'            Else
'                ISReal = False
    End If
    


EndNow:
End Function
 

Watch MrExcel Video

Forum statistics

Threads
1,095,481
Messages
5,444,736
Members
405,298
Latest member
fxtrtr17

This Week's Hot Topics

Top