Check if it is connected to the Internet

182 views Asked by At

I am creating an operating system and there will be updates. I want VBA to check if there is an active Internet connection through WiFi or Ethernet. If the check is successful, then it sends you to a certain slide, but if it failed, then it sends you to another slide

This is the code I tried and it's not working:

Private Function IsInternetConnected(Optional SupressMessage As Boolean) As Boolean

Dim objHTTP As Object

'Test for Internet Connection
  If IsInternetConnected = True Then
  ActivePresentation.SlideShowWindow.View.GotoSlide 3
End

'Report to User if Internet Connection not detected
  If IsInternetConnected = False And SupressMessage = False Then
    ActivePresentation.SlideShowWindow.View.GotoSlide 2
  End If

End Function
2

There are 2 answers

2
Tzipirigu Mic On BEST ANSWER

I managed to make the code work. The only thing I had to do, not specified anywhere, was to add a shape to which I assigned the action that would start the macro. Here is the code:

Sub InternetCheck()

    Dim objSlide As Slide
    Dim objShape As Shape
    Dim strURL As String
    Dim blnConnected As Boolean

    strURL = "http://www.bing.com"

    On Error Resume Next
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    objXMLHTTP.Open "GET", strURL, False
    objXMLHTTP.Send
    If objXMLHTTP.Status = 200 Then
        blnConnected = True
    Else
        blnConnected = False
    End If
    On Error GoTo 0

    If blnConnected Then
        ActivePresentation.SlideShowWindow.View.GotoSlide (3)
    Else
        ActivePresentation.SlideShowWindow.View.GotoSlide (2)
    End If

End Sub

To start the code, it must be automated with a module that calls the macro.

14
jacouh On

You might do this, using Windows API:


#If Win64 Then
  Private Declare PtrSafe Function InternetGetConnectedState _
    Lib "wininet.dll" (lpdwFlags As LongPtr, _
      ByVal dwReserved As Long) As Boolean
#Else
  Private Declare Function InternetGetConnectedState _
    Lib "wininet.dll" (lpdwFlags As Long, _
      ByVal dwReserved As Long) As Boolean
#End If

Private Function IsInternetConnected(Optional SupressMessage As Boolean) As Boolean

    Dim bConnected As Boolean

    bConnected = InternetGetConnectedState(0&, 0&)
    'Test for Internet Connection
    If bConnected Then
        ActivePresentation.SlideShowWindow.View.GotoSlide 3
    End If

    'Report to User if Internet Connection not detected
    If Not bConnected And Not SupressMessage Then
        ActivePresentation.SlideShowWindow.View.GotoSlide 2
    End If

    IsInternetConnected = bConnected
End Function

Ref.

  1. https://learn.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-internetgetconnectedstate
  2. Check for active internet connection

enter image description here After testing

  • with Ethernet cable disconnected, InternetGetConnectedState(0&, 0&) returns false,
  • on the other hand, with Ethernet cable connected again to my PC, InternetGetConnectedState(0&, 0&) returns true.

This Windows DLL function works fine.