1

Below code is to check whether a network connection on the machine is established.

Dim T = 0

While(T = 0)

    Set Http = WScript.CreateObject("MSXML2.ServerXMLHTTP")
    Http.Open "GET", "http://www.google.com/", True
    Http.Send

        If(Http.Status = 200) Then 
           MsgBox "Network Connection is established"

           T = 1

        else 
           MsgBox "Network Connection isn't established yet"

        End If
Wend

Whether or not network is connected, the Http.Status returns value 200. Could anyone assist whether I'm missing anything here.

  • Code should be indented by four spaces (and not surrounded by `+` characters). @TZHX edited for you this time, but please try to format it properly in future questions. – Ironcache Apr 25 '18 at 10:30
  • Have you tried with CreateObject("WinHttp.WinHttpRequest.5.1") instead? – Regis Desrosiers Apr 25 '18 at 13:53

1 Answers1

0

Refer to this ==> Loop a function?

You can do it easily with this code :

Option Explicit
Dim MyLoop,strComputer,objPing,objStatus
MyLoop = True
While MyLoop = True
    strComputer = "smtp.gmail.com"
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery _
    ("select * from Win32_PingStatus where address = '" & strComputer & "'")
    For Each objStatus in objPing
        If objStatus.Statuscode = 0 Then
            MyLoop = False
            Call MyProgram()
            wscript.quit
        End If
    Next
    Pause(10) 'To sleep for 10 secondes
Wend
'**********************************************************************************************
 Sub Pause(NSeconds)
    Wscript.Sleep(NSeconds*1000)
 End Sub
'**********************************************************************************************
Sub MyProgram()
Dim WshShell
set WshShell = WScript.CreateObject("WScript.Shell")         
On Error Resume Next
   With WScript.CreateObject ("InternetExplorer.Application")     
      .Navigate "http://www.example.com/slideshow"
      .fullscreen = 1   
      .Visible    = 1
      WScript.Sleep 10000
   End With    
On Error Goto 0
End Sub
'**********************************************************************************************

EDIT :

Wscript.echo(CheckConnection("https://www.google.com"))
'----------------------------------------------------
Function CheckConnection(URL)
    On Error Resume Next 'swallow errors
    Set o = CreateObject("MSXML2.XMLHTTP") 
    o.open "GET",URL, False 
    o.send 
    If Err.Number <> 0 Then
        CheckConnection = "An error occured. Data not retrieved."
'or whatever else you want to do in such a case.
    Else
        CheckConnection = "You are connected"
    End If
    On Error Goto 0 'back to normal error behaviour
End Function
'----------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Thanks Hackoo. It works fine on the machine which is connected to internet. But I have another environment Intranet (no internet) and Ping is disabled in that environment. Tried the same script. Replaced "smtp.gmail.com" with intranet website link but script isn't working. will there be any alternative here. Thanks – user9684191 Apr 26 '18 at 05:06
  • Hi Hackoo, could you help me here. Thanks – user9684191 Apr 27 '18 at 02:54
  • Can I use any alternative command instead of Ping in the above script? Please confirm. – user9684191 Apr 27 '18 at 03:16