title: Check internet connection from Excel VBA
body: <p>I need to check the Internet connection when an Excel workbook opens. For that, I'm trying the following code:</p>
  
  <pre><code>Private Declare PtrSafe Function InternetGetConnectedState _
     Lib "wininet.dll" (ByRef dwflags As Long, _
     ByVal dwReserved As Long) As Long
  
  Private Const INTERNET_CONNECTION_MODEM As Long = &amp;H1
  Private Const INTERNET_CONNECTION_LAN As Long = &amp;H2
  Private Const INTERNET_CONNECTION_PROXY As Long = &amp;H4
  Private Const INTERNET_CONNECTION_OFFLINE As Long = &amp;H20
  Function IsInternetConnected() As Boolean
      Dim L As Long
      Dim R As Long
      R = InternetGetConnectedState(L, 0&amp;)
      If R = 0 Then
          IsInternetConnected = False
      Else
          If R &lt;= 4 Then
              IsInternetConnected = True
          Else
              IsInternetConnected = False
          End If
      End If
  End Function
  </code></pre>
  
  <p>It works, but some users are reporting this is breaking Excel (it freezes Excel forever). </p>
  
  <p>Is there a way to throw an error or somehow correct this code in order to find out what is the cause of the problem (Firewall, etc.)?</p>
  
  <p>Thank you so much for helping!</p>