In the office, Excel is a way of life… not just a tool


Use VBA to test the internet connection

In some of my applications I utilise the IE web browser control to display reports, some of which request internet content (e.g. Google Chart API and Google Maps API).

The trouble is, you can't always guarantee that the users PC has an internet connection. So, what's the best approach to the problem? Well, check the internet connection using the handy bit if VBA code below!

Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long
'Testing for internet connection
Public Function IsInternetConnected() As Boolean
    IsInternetConnected = InternetGetConnectedStateEx(0, "", 254, 0)
End Function
Filed under: Userforms, VBA 4 Comments