Checking for an internet connection with Excel

Sometimes we have a project that requires an internet connection.  Of course, before charging ahead and USING the connection, it’s safest to first test to see if there IS a connection!  This module uses Windows’ built-in “wininet” to query a website and let us know if it’s available.  We use a high-availability one like Google or Yahoo.

We also use this in error checking to see if a particular website is up.  For example, if we’re trying to use Excel to download and process a file from our web server, we first test GravityComputing.co.nz to see if we can reach it.  If we can’t, then we check Google.  If they’re both down, it’s pretty safe to assume that the internet connection isn’t working.  However if only our site is down, we know exactly what point to look into.

NOTE: The following code has been redeveloped from the version posted on VBnet:

http://vbnet.mvps.org/index.html?code/internet/internetcheckconnection.htm

 

Option Explicit

Private Const FLAG_ICC_FORCE_CONNECTION = &H1

Private Declare Function InternetCheckConnection Lib "wininet.dll"
Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal
dwFlags As Long, ByVal dwReserved As Long) As Long

Private Function ConnectedToInternet() As Boolean

   ConnectedToInternet = InternetCheckConnection("http://www.yahoo.com/",
FLAG_ICC_FORCE_CONNECTION, 0&)

End Function

Private Function TestConnection()

    MsgBox ConnectedToInternet

End Function