- 帖子
- 2035
- 主題
- 24
- 精華
- 0
- 積分
- 2031
- 點名
- 0
- 作業系統
- Win7
- 軟體版本
- Office2010
- 閱讀權限
- 100
- 性別
- 男
- 註冊時間
- 2012-3-22
- 最後登錄
- 2024-2-1
|
回復 c_c_lai
加入doevents還是不行。果然是找不到command指令,不知有無其它替代方法?不好意思,下班了 ...
linderlong 發表於 2013-8-30 16:59 
閒閒無事,貼上幾個 Function, 自己看看哪種適用。
1. 測試主程式:- Sub TestPing()
- Dim xi As Integer
-
- xi = 1
- Do
- If Cells(xi, 1) <> "" Then
- ' MsgBox sPing("www.microsoft.com")
- ' Cells(xi, "C") = sPing("www.google.com")
- Cells(xi, "C") = Ping2("www.google.com")
- ' Cells(xi, "C") = sPing(Cells(xi, 1))
- ' Cells(xi, "C") = IsConnectible(Cells(xi, 1))
- End If
- xi = xi + 1
- Loop Until Cells(xi, 1) = ""
- End Sub
複製代碼 以下為程式模組,你可以一一測測看:- Private Function Ping2(strAddr As String) As String
- Dim strTmpFile As String
-
- strTmpFile = Environ("TEMP") & "\PingResult.txt" ' 準備建暫存檔在 Windows Temp 目錄下
- ' 建立 WScript 物件 Shell 類別 , 使用 Run 方法 , 呼叫外部指令 Ping ( 並等待其執行結束 )
- CreateObject("WScript.Shell").Run Environ("COMSPEC") & " /c PING " & strAddr & " > " & strTmpFile, 0, True
- ' PS : "Ping IP位置 > XX檔案" 這裡是使用 > 來將 Ping 結果寫到檔案裡
- ' ( 當然也可 Call API CreatePipe 來讀取命令結果 )
- ' Ping 是一種公用程式,可以驗證一或多個遠端主機連線。
- ' Ping 指令用 ICMP 回應要求和回應回覆套裝軟體來決定網路上的特殊 IP 系統是否正常運作。
- ' Ping 對診斷 IP 網路或路由器失敗非常有用。
- ' Internet Control Message Protocol (ICMP)
- ' TCP/IP 組件中必要的一種維護通訊協定,可報告錯誤並允許簡易連線。
- ' Ping 工具用來執行 TCP/IP 疑難排解的 ICMP。
- ' 詳細說明及用法可參考:
- ' ms-its:%WINDIR%\Help\ntcmds.chm::/ping.htm
- ' ms-its:%WINDIR%\Help\ntcmds.chm::/ping.htm
- ' ms-its:%WINDIR%\Help\tcpip.chm::/sag_TCPIP_pro_PingConnect.htm
- ' ms-its:%WINDIR%\Help\tcpip.chm::/sag_TCPIP_pro_Ping.htm
- ' 建立檔案系統物件,用來開啟暫存文字檔,並讀取內容 ( 取回 Ping的結果 )
- Ping2 = CreateObject("Scripting.FileSystemObject").OpenTextFile(strTmpFile).ReadAll
- Ping2 = Replace(Ping2, vbCrLf, "", 1)
-
- On Error Resume Next
- Kill strTmpFile ' 刪除暫存檔
- End Function
複製代碼 |
|