1
votes

I am trying to construct a VBScript file to remind teachers (once they have logged in) in the school I am in to take the register when it needs to be taken (in this case, 15 minutes into each lesson).

I am doing this by an attempted infinite loop and checking what the time is to then alert the user. However, when run, the condition is only met when the time at execution equals 9:00 or 10:00 etc. I would like it to run, effectively, in the background and alert whenever the time is equal to 9:00 or 10:00 etc.

The code is below:

Do While True
    If Now() = #09:00# Or Now() >= #10:00# Or Now() >= #11:20# Or Now() >= #12:20# Or Now() >= #14:05# Then
        msg = Msgbox ("Have you done your register?",4+48+4096,"")
        If msg = 7 Then
            Msgbox "Please take it immediately.",48+4096,""
            Exit Do
        Else
            Exit Do
        End If
    End If
Loop

I have tried getting time differently through FormatDateTime() and a Do Until loop with the same results.

Obviously, if I were to get rid of the Exit Do statement, it would run forever during that minute. I would like it to only alert once at each time.

Please help!

2

2 Answers

1
votes

It works!

I managed to change the script so that the proper time is read and the script must wait a minute to get out of the msgbox loop. Then upon reading the last time, it shows the last message and ends the loop, however, it will still run in the background until the person logs off (I don't think it will put a strain on resources).

Do While True
    ctime = FormatDateTime(Now(), 4)
    If ctime = "09:00" Or ctime = "10:00" Or ctime = "11:20" Or ctime = "12:20" Then
        msg = Msgbox ("Have you done your register?",4+32+4096,"")
        If msg = 7 Then
            Msgbox "Please take it immediately.",48+4096,""
            WScript.CreateObject("WScript.Shell").Run(path)
            WScript.Sleep 60000
        Else
            WScript.Sleep 60000
        End If
    ElseIf ctime = "14:05" Then
        msg = Msgbox ("Have you done your register?",4+32+4096,"")
        If msg = 7 Then
            Msgbox "Please take it immediately.",48+4096,""
            WScript.CreateObject("WScript.Shell").Run(path)
            WScript.Quit
            Exit Do
        Else
            WScript.Quit
            Exit Do
        End If
    End If
Loop
1
votes

You should write like this :

Do While True
    ctime = FormatDateTime(Now(),4)
    If ctime = "09:00" Or ctime = "10:00" Or ctime = "11:20" Or ctime = "12:20" Then
        msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
        If msg = VbNo Then
            Msgbox "Please take it immediately.",48+4096,Now()
            WScript.Sleep 60000
        Else
            WScript.Sleep 60000
        End If
    ElseIf ctime = "14:05" Then
        msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
        If msg = VbNo Then
            Msgbox "Please take it immediately !",48+4096,Now()
            Exit Do
            WScript.Quit
        Else
            Exit Do
            WScript.Quit
        End If
    End If
Loop

I add some funny stuff like to be get alarmed by an alarm from the net, just give a try if you like !

Option Explicit
If AppPrevInstance() Then 
    MsgBox "There is an existing proceeding !" & VbCrLF &_
    CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else 
    Dim ctime,msg,Time1,Time2,Time3,Time4,LastTime2GoAway,Alarm
    Time1 = "09:00" : Time2 = "10:00" : Time3 = "11:20" : Time4 = "12:20" : LastTime2GoAway = "14:05"
    Alarm = "http://soundbible.com/1080-Airhorn.html"
    Do While True
        ctime = FormatDateTime(Now(),4)
        If ctime = Time1 Or ctime = Time2 Or ctime = Time3 Or ctime = Time4 Then
            If CheckConnectionInternet() = True Then 
                Call Alert(Alarm)
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately.",48+4096,Now()
                    Call Pause(60)
                Else
                    Call Pause(60)
                End If
            Else    
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately.",48+4096,Now()
                    Call Pause(60)
                Else
                    Call Pause(60)
                End If
            End If
        ElseIf ctime = LastTime2GoAway Then
            If CheckConnectionInternet() = True Then 
                Call Alert(Alarm)
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately !",48+4096,Now()
                    Exit Do
                    WScript.Quit
                Else
                    Exit Do
                    WScript.Quit
                End If
            Else
                msg = Msgbox ("Have you done your register ?",4+32+4096,Now())
                If msg = VbNo Then
                    Msgbox "Please take it immediately !",48+4096,Now()
                    Exit Do
                    WScript.Quit
                Else
                    Exit Do
                    WScript.Quit
                End If
            End If
        End If
    Loop
End If
'**************************************************************************
Sub Alert(WebSite)
    Dim objExplorer
    Set objExplorer = CreateObject("InternetExplorer.Application")
    objExplorer.Navigate WebSite
    objExplorer.Visible = 0 'Invisible
    Do While (objExplorer.Busy)
        Wscript.Sleep 500
    Loop
    Wscript.Sleep 5000
    objExplorer.Quit
End Sub
'**************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function    
'**************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'**************************************************************************
Function CheckConnectionInternet()
    Dim strComputer,objPing,objStatus
        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
                CheckConnectionInternet = True
            Else
                CheckConnectionInternet = False
            End If
        Next
End Function
'**********************************************************************************************
Sub Pause(NSeconds)
    Wscript.Sleep(NSeconds*1000)
End Sub
'**************************************************************************************