check internet availability using VBA

i need to check the availability of a internet connection frequently and need to report it in excel sheet.
how can i implement this task using excel VBA? please help...
Answer
Answer

The following code will return True if there is an internet connection or False if there is no connection.

 

Private Declare Function InternetGetConnectedState _
   Lib "wininet.dll" (ByRef dwflags As Long, _
   ByVal dwReserved As Long) As Long

Private Const INTERNET_CONNECTION_MODEM As Long = &H1
Private Const INTERNET_CONNECTION_LAN As Long = &H2
Private Const INTERNET_CONNECTION_PROXY As Long = &H4
Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20

Function IsInternetConnected() As Boolean
    Dim L As Long
    Dim R As Long
    R = InternetGetConnectedState(L, 0&)
    If R = 0 Then
        IsInternetConnected = False
    Else
        If R <= 4 Then
            IsInternetConnected = True
        Else
            IsInternetConnected = False
        End If
    End If
End Function

You would call this in your code with something like

 

Sub AAA()
    If IsInternetConnected() = True Then
        ' connected
    Else
        ' no connected
    End If
End Sub

 

For code and examples for using timer loops, see www.cpearson.com/Excel/OnTime.aspx . This describes the OnTime procedure that you can use to repeatedly schedule code to execute.

 

Cordially,
Chip Pearson
Excel MVP 1998 - 2014
Pearson Software Consulting, LLC
www.cpearson.com

5 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated April 26, 2020 Views 11,598 Applies to: