Search code examples
excelinternet-explorervbaxmlhttprequest

Handle lack of internet connection in Visual Basic for Applications


I am creating simple code which goes to a website, clicks on a login button, enters login details etc., and downloads data. I know this is not a recommended method, but using Microsoft.XMLHTTP where I GET the URL doesn't help as the URL of the page I finally reach, after login, has a unique authentication token generated. E.g. yesterday, URL = www.abcd.com/ABCD-EFGH-IJKL and today it is URL = www.abcd.com/MNOP-QRST-UVWX, so I don't have a set URL. Therefore, with the InternetExplorer.Application method, I am trying to handle disconnections from the internet. Being disconnected from the internet, does not generate an error, but merely a blank page, so I can't use On Error GoTo LineX. How do I go about handling a disconnection in a code such as:

Dim IE as Object
Set IE = CreateObject (InternetExplorer.Application")
IE.navigate "www.abcd.com"
If 'some sort of status response' = 'some value' Then Msgbox 'You are disconnected from the internet'
Else
End If

Is there a status response code (as there is with Microsoft.XMPHTTP) which I can use to inform user with msgbox that "Please connect to the internet etc."?

Thank you.


Solution

  • I'm not familiar with a way to get the actual HTTP response, but you could check for some sort of element on the page (or if there are elements at all), like so:

    elements = IE.document.getElementsByTagName("input") 'Or whatever element will be on that page
    If elements.Length = 0 Then 
        MsgBox "Please connect to the Internet."
    End If