【VBA】Run ping command

You can run “ping command“!

PR

VBA Code

For example, run ping commando to host name “serverhost”.

Option Explicit

Sub sampleProc()
    
    Dim objLocator As Object
    Dim objServer As Object
    Dim objPingSet As Object
    Dim ping As Object
    
    Set objLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objServer = objLocator.ConnectServer()
    
    'Set "host name" or "IP address" as a condition
    Set objPingSet = objServer.ExecQuery _
                     ("Select * From Win32_PingStatus Where Address = 'serverhost'")
    
    'Run ping command
    For Each ping In objPingSet
        Select Case ping.StatusCode
        Case 0
            MsgBox ("Success")
        Case 11010
            MsgBox ("failed【Timeout】")
        Case 11003
            MsgBox ("failed【Unreachable】")
        Case Else
            MsgBox ("failed")
        End Select
    Next
    
    Set objLocator = Nothing
    Set objServer = Nothing
    Set objPingSet = Nothing
    
End Sub

Set “host name” or “IP address” as a condition(Line 15).

Run ping command(Line 18~29).

PR

Result

You ran “ping command“!

Result
Result
PR

FYI

For more information about following, see the following official online manual:

ConnectServer method” of “SWbemLocator Object”


ExecQuery method” of “SWbemServices Object”


●Ping result details

タイトルとURLをコピーしました