VBA to determine if computer has a web connection

sefMI

Board Regular
Joined
Apr 24, 2006
Messages
127
Is it possible to have a macro determine if the computer has internet access/connection?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
A few ways:


1)


'Standard Module code, like: Module1!
Public Declare Function InternetGetConnectedState _
Lib "wininet.dll" (lpdwFlags As Long, _
ByVal dwReserved As Long) As Boolean


Function IEAvailable() As Boolean
'Standard Module code, like: Module1!
Dim IEStat As Long

IEAvailable = (InternetGetConnectedState(IEStat, 0&) <> 0)
End Function

Sub isInternetAvailable()
'Standard Module code, like: Module1!

If IEAvailable = False Then MsgBox "No, Internet connection is available, at this time!"
If IEAvailable = True Then MsgBox "An Internet connection is available, at this time!"
End Sub


2)


Sub IEOpen()
'Open Internet Explorer as a new window!
Dim ie As Object

Set ie = CreateObject("internetexplorer.application")

'ie.Visible = True

With ie
'Open Internet Explorer Window!
.Visible = True

'Optional: Open with this web-page!
.Navigate "http://infonetWebSpace"
End With

Set ie = Nothing
End Sub


Sub IEOpen2()
'Open, do not make active window

Shell "C:\Program Files\Internet Explorer\iexplore.exe http://infonetWebSpace"
End Sub



3)


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


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


Private Const FLAG_ICC_FORCE_CONNECTION = &H1
'// Reference this msg!
'Private Const strSite As String = _
' "http://www.mrexcel.com/board2/viewtopic.php?
t=157314&postdays=0&postorder=asc&start=10"
Private Const strSite As String = "http://www.mrexcel.com/board2"

Dim strISPName As String * 255

Sub CheckConnection()
Dim Ret As Long
Ret = InternetGetConnectedStateEx(Ret, strISPName, 254, 0)

If InternetCheckConnection(strSite, FLAG_ICC_FORCE_CONNECTION, 0&) = 0 Then
MsgBox "You are NOT connected", vbInformation
Else
MsgBox "You ARE connected to " & strSite & vbCrLf & "Via: " & strISPName, vbInformation
End If
End Sub
 
Upvote 0
I tried number 1 and it gave me an error
Constants, fixed-length strings, arrays, user-defined types, and Declare statements not allowed as Public members of an object module

I'm using this "Feature" for an on open procedure.
 
Upvote 0
You need to open the VBA Editor and from its ToolBar: Insert - Module

This code will need to be placed in a Standard module.
Module Level: Describes code in the Declarations section of a code module. Any code outside a procedure [Sub/Function] is referred to as module-level code [Dim, Cnst, Public, Private]. All Declarations must be listed first and before procedures. Module-Level code must be followed by procedures.

So the code must be pasted as is and at the top of a module!

Did you paste the code into a clean Module, as posted?

To get this to work in the "ThisWorkbook" code module as an "Open" Event, Put the posted code into a Standard code module, like: Module1
Then in the ThisWorkbook module Open Event

add:

Call isInternetAvailable
or
Application.Run "isInternetAvailable"
or
isInternetAvailable

I use just the Subs name, like: isInternetAvailable
Then save, close, open and it should work!

Private Sub Workbook_Open()
'ThisWorkbook code, only!

isInternetAvailable
End Sub


I checked each code set and they all worked for me!
 
Upvote 0
Check my post above this one, I updated it to account for the ThisWorkbook Open Event!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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