Link to home
Start Free TrialLog in
Avatar of Bright01
Bright01Flag for United States of America

asked on

Checking for Internet Connection

EE Pros,

I have a Macro that updates MSN Currency when fired.  



Is there a way to check to see that a connection exists and if not, notify the user that "No Internet Connection Detected".

Sub CurrencyUpdate()
Sheets("Currency").QueryTables(1).Refresh
End Sub

That's it!

Thank you in advance,

B.
Avatar of Chris Millard
Chris Millard
Flag of United Kingdom of Great Britain and Northern Ireland image

What you could do is use VBA to check to see if a URL is valid - see this post which contains an example  that may be of help:-

http://www.mrexcel.com/forum/excel-questions/567315-check-if-url-exists-so-then-return-true.html
You can use something like the following code:

Which in short says "If internet is active then refresh data, else notify user..."

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
Sub CurrencyUpdate()
    If IsInternetConnected() = True Then
        Sheets("Currency").QueryTables(1).Refresh
    Else
        MsgBox "No internet connection is detected!"
    End If
End Sub

Open in new window

SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bright01

ASKER

Rgonzo and ThinkSpace,

Where do I put this code?  In the WS or a Module?

B.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  Both solutions work...... good teamwork guys.

B.