Excel VBA如何编码可判断当前是否网络连接?

来源:百度知道 编辑:UC知道 时间:2024/05/31 21:56:21
最好是当点击某个单元格如C5时,自动后台判断。若未连接,返回提示信息“请保持网络连接”;若已连接,则不返回信息。

Public Function Pings(strMachines As String) As Boolean
aMachines = Split(strMachines, ";")
For Each machine In aMachines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & machine & "'")
For Each objStatus In objPing
If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
Debug.Print ("machine " & machine & " is not reachable")
Pings = False
Else
Pings = True
End If
Next
Next
End Function

调用这个函数 就何以了
例如:Pings("www.baidu.com") 连接了返回TRUE,反之为FALSE。