vieirasoft 18/2/2011, 21:54
Tem vários exemplos aqui no fórum. Eu uso assim:
Abrir um módulo em VBA
'Declares for direct ping
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Long
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Dim X As Boolean
Dim hInet As Long
Dim hUrl As Long
Dim flags As Long
Dim URL As Variant
Public Function CheckConnection()
hInet = InternetOpen(" ", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
If hInet Then
flags = INTERNET_FLAG_KEEP_CONNECTION Or INTERNET_FLAG_NO_CACHE_WRITE Or INTERNET_FLAG_RELOAD
hUrl = InternetOpenUrl(hInet, "http://www.yahoo.com", vbNullString, 0, flags, 0)
If hUrl Then
MsgBox "O seu computador está conectado à Internet", vbInformation, "Verificando Conexão"
Call InternetCloseHandle(hUrl)
Else
MsgBox "O seu computador não está conectado à Internet" & vbCr & "Deve conectar-se à internet para continuar.", vbInformation, "Verificando conexão"
X = False
End If
End If
Call InternetCloseHandle(hInet)
End Function
E depois chamara função, por exemplo num botão de comando
Call CheckConnection