Advertisement
Kermer

VBA - ping address

Apr 12th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Checks if ping successfully reached specified address
  2. Public Function IsAddressReachable(ByVal url As String) As Boolean
  3.     Dim host As String
  4.     host = ExtractDomain(url)
  5.     IsAddressReachable = IsHostReachable(host)
  6. End Function
  7.  
  8. ' Checks if ping successfully reached specified host (domain)
  9. Public Function IsHostReachable(ByVal host As String) As Boolean
  10.     Dim pingResult As String
  11.     pingResult = GetPingResult(host)
  12.     If pingResult = "Connected" Then
  13.         IsHostReachable = True
  14.     Else
  15.         IsHostReachable = False
  16.     End If
  17. End Function
  18.  
  19. ' Extracts domain (host) from url address.
  20. Public Function ExtractDomain(ByVal url As String) As String
  21.     If InStr(url, "//") Then
  22.         url = Mid(url, InStr(url, "//") + 2)
  23.     End If
  24.     If Left(url, 4) Like "[Ww][Ww][Ww0-9]." Then
  25.         url = Mid(url, 5)
  26.     End If
  27.     ExtractDomain = Split(url, "/")(0)
  28. End Function
  29.  
  30. ' Pings specific host.
  31. Public Function GetPingResult(ByVal host As String) As String
  32.     Dim objPing As Object
  33.     Dim objStatus As Object
  34.     Dim strResult As String
  35.    
  36.     host = Replace(host, "'", "")
  37.  
  38.     Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
  39.         ExecQuery("Select * from Win32_PingStatus Where Address = '" & host & "'")
  40.          
  41.     'report the results
  42.    For Each objStatus In objPing
  43.         Select Case objStatus.StatusCode
  44.             Case 0: strResult = "Connected"
  45.             Case 11001: strResult = "Buffer too small"
  46.             Case 11002: strResult = "Destination net unreachable"
  47.             Case 11003: strResult = "Destination host unreachable"
  48.             Case 11004: strResult = "Destination protocol unreachable"
  49.             Case 11005: strResult = "Destination port unreachable"
  50.             Case 11006: strResult = "No resources"
  51.             Case 11007: strResult = "Bad option"
  52.             Case 11008: strResult = "Hardware error"
  53.             Case 11009: strResult = "Packet too big"
  54.             Case 11010: strResult = "Request timed out"
  55.             Case 11011: strResult = "Bad request"
  56.             Case 11012: strResult = "Bad route"
  57.             Case 11013: strResult = "Time-To-Live (TTL) expired transit"
  58.             Case 11014: strResult = "Time-To-Live (TTL) expired reassembly"
  59.             Case 11015: strResult = "Parameter problem"
  60.             Case 11016: strResult = "Source quench"
  61.             Case 11017: strResult = "Option too big"
  62.             Case 11018: strResult = "Bad destination"
  63.             Case 11032: strResult = "Negotiating IPSEC"
  64.             Case 11050: strResult = "General failure"
  65.             Case Else: strResult = "Unknown host"
  66.         End Select
  67.         GetPingResult = strResult
  68.     Next
  69.  
  70.     Set objPing = Nothing
  71. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement