Tag Archives: Conexão

VBA – Verificar conexão com a Internet

Internet Connection

Método simples para verificar se o computador está ou não conectado a Internet por VBA.

Copie e cole o seguinte código em um módulo VBA:

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
 
Dim sConnType As String * 255
 
Sub TesteConexaoInternet()
    Dim Ret As Long
    Ret = InternetGetConnectedStateEx(Ret, sConnType, 254, 0)
    If Ret = 1 Then
        MsgBox "Você está conectado a Internet via " & sConnType, vbInformation
    Else
        MsgBox "Você não está conectado a Internet", vbInformation
    End If
End Sub

A chamada retorna uma mensagem que avisa se existe conexão com a Internet e através de qual conexão ela é feita.

Abraços

Tomás