Run function?

Is it possible to loop a function until element = TRUE?

I am trying to ping the server ... After establishing a connection or Ping = TRUE, the program will execute. If no connection is established, the ping will be repeated until it is TRUE.

My code so far is below. If TRUE, MyProgram will open. If False, the function will be called again. But that doesn't happen ... nothing really happens, it just comes out.

Any help is gladly appreciated. If anyone knows of a more efficient way to accomplish this task then please let me know. Thank!

     Function Ping
        Dim oPing, oRetStatus, bReturn
        Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & "strHost" & "'")

        For Each oRetStatus In oPing
            If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
                bReturn = False
            Else
                bReturn = True
            End If
            Set oRetStatus = Nothing
        Next
        Set oPing = Nothing

    Ping = bReturn
    End Function

If Ping Then
    Call MyProgram
Else
    Call PingSub
End If

Sub MyProgram
    Dim objShell
        Set objShell = WScript.CreateObject( "WScript.Shell" )
        objShell.Run("\\Path\To\My\Program.exe")
        Set objShell = Nothing
End Sub

Sub PingSub
    Call Ping
End Sub

      

+1


source to share


2 answers


Try it like this:



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 objShell
Set objShell = CreateObject( "WScript.Shell" )
objShell.Run("calc.exe")
Set objShell = Nothing
End Sub
'**********************************************************************************************

      

+5


source


It will be a recursive function.

https://msdn.microsoft.com/en-us/library/81tad23s.aspx I assume you are using VBA



here is an example from MS

0


source







All Articles