2015-11-04 64 views
-1

我完全不瞭解VB,但我試圖在word文檔中編譯一個快速的ping測試宏來測試一些惡意軟件沙盒軟件。但是,我不斷收到運行時424錯誤。VB ping宏 - 需要的對象

我已經做了一些研究,但對VB的0知識,我沒有找到一個解決方案。代碼如下。

Sub Ping() 

' Ping Macro 

If My.Computer.Network.Ping("192.168.1.10") Then 
    MsgBox ("Server pinged successfully.") 
Else 
    MsgBox ("Ping request timed out.") 
End If 

End Sub 

我很明顯在這裏丟失了一些東西。我認爲這個對象應該是消息框,但我錯了。有人知道我在這裏想念什麼嗎?

編輯:調試顯示第一行是問題。

If My.Computer.Network.Ping("192.168.1.10") Then 

謝謝。

+0

嘿那裏,看看這個職位。 http://stackoverflow.com/questions/21020077/ping-ip-address-with-vba-code-and-return-results-in-excel –

+0

對於Excel電子表格肯定。我仍然不知道自己在用VBA做什麼,這會如何轉化爲Word宏?我對錯誤處理沒有興趣,我從字面上只需要ping和觀察虛擬機中的流量。 – Sevaara

回答

2

My.Computer.Network.Ping()是一個VB.Net函數,不適用於VBA。

從前段時間我有一個函數來獲取ping時間,這應該讓你去。
您可能只需要StatusCode = 0檢查。

' strAddress = IP or name 
Public Function PingTime(strAddress) As Long 

    Dim objPing As Object, objStatus As Object 

    ' Init: Assume error 
    PingTime = -1 

    On Error Resume Next 
    Set objPing = GetObject("WinMgmts:{impersonationLevel=impersonate}").ExecQuery _ 
          ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strAddress & "' ") 
    If Err.Number <> 0 Then 
     Exit Function 
    End If 

    For Each objStatus In objPing 
     If objStatus.StatusCode = 0 Then 
      PingTime = objStatus.Properties_("ResponseTime").Value 
      Exit For 
     End If 
    Next 
    Set objStatus = Nothing 
    Set objPing = Nothing 

End Function