VBA check Internet connections

nd0911

Board Regular
Joined
Jan 1, 2014
Messages
166
Hello,

I'm looking for a VBA code that returns if there is an internet connections.

There is a lots of code out there but the problom is that I didnt found a code that actualy works no matter what type of Excel version the user have (I mean 32 BIT or 64 BIT).

I will be happy for help on this issue.

Thank you.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi,


See if this code does what you want

Place in a standard module

Code:
'live & not just a physical connection to the internet.
'Flags for GetInternetState, these are returned


Public Const INTERNET_CONNECTION_MODEM As Long = &H1
Public Const INTERNET_CONNECTION_LAN As Long = &H2
Public Const INTERNET_CONNECTION_PROXY As Long = &H4
Public Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8
Public Const INTERNET_RAS_INSTALLED As Long = &H10
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40


Public Declare Function GetInternetState _
Lib "Wininet.dll" _
Alias "InternetGetConnectedState" _
(ByVal lpdwFlags As Long, _
ByVal Reserved As Long) As Long




Public Function IsInternetLive() As Boolean
    
    Dim Flags As Long
    Dim Ret
    
    Ret = GetInternetState(Flags, 0&)
    IsInternetLive = Ret And Not (Flags And INTERNET_CONNECTION_OFFLINE)
    
End Function


Sub CheckConnection()
    
    If IsInternetLive Then
        MsgBox "connected to the Internet."
    Else
        MsgBox "not connected to the Internet."
    End If
    
End Sub

I tested it on my 64 bit machine & seemed to return correct result.

Note: This is not code I have developed & my apologies to the author for not giving the credit but it was in public domain - I have just lost the link to the original source.

Dave
 
Upvote 0
Thank you for your answer !

I just tested your code and its always returns TRUE even when I unplug my internet cable.

is it the same for you ?
 
Upvote 0
Thank you for your answer !

I just tested your code and its always returns TRUE even when I unplug my internet cable.

is it the same for you ?

Its not my code, just code came across few years ago but lost link to original source.

Just tried that & yep same result - maybe what i have is not complete & sorry but not able to suggest any updates.
Perhaps another here can offer further suggestions.

Dave
 
Last edited:
Upvote 0
I found this one here:
https://stackoverflow.com/questions/551613/check-for-active-internet-connection

It returns true/false correctly for my internet connection



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

Public Function GetInternetConnectedState() As Boolean
  GetInternetConnectedState = InternetGetConnectedState(0&, 0&)
End Function

Sub Get_State()

MsgBox (GetInternetConnectedState)

End Sub
 
Upvote 0
dmt32, Thank you very much for sharing. I tested your code and it returned true/false correctly for my internet connection

welcome to forum.
As I clearly stated in my original post - the code provided was not developed by myself & unfortunately, I have lost the link to original author to give credit.

Glad solution does work for you

Dave
 
Upvote 0
I found this one here:
Check for active internet connection

It returns true/false correctly for my internet connection



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

Public Function GetInternetConnectedState() As Boolean
  GetInternetConnectedState = InternetGetConnectedState(0&, 0&)
End Function

Sub Get_State()

MsgBox (GetInternetConnectedState)

End Sub
Worked for me!!. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top