Loop a function?

前端 未结 2 543
离开以前
离开以前 2020-12-12 03:33

Is it possible to loop a function until an item = TRUE?

I am trying to ping a server... Once connection is established, or Ping = TRUE, then a program will execute.

相关标签:
2条回答
  • 2020-12-12 04:18

    That would 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 讨论(0)
  • 2020-12-12 04:19

    Try 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
    '**********************************************************************************************
    
    0 讨论(0)
提交回复
热议问题