Reputation: 1
So, I'm doing this watch program. I have a loop that I use to continuously update time. But for some reason, the system doesn't recognise the Do
at start of loop. It is in a while loop, if that changes anything. I also put a On Error Resume Next
but still get error message. Is this linked? Help would be greatly appreciated. Here's source code:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "about:blank"
While ie.ReadyState <> 4 : WScript.Sleep 100 : Wend
ie.ToolBar = False
ie.StatusBar = False
ie.Width = 200
ie.Height = 250
ie.document.body.innerHTML = "<p id='msg'>0</p>"
Set style = ie.document.CreateStyleSheet
style.AddRule "p", "text-align: center;"
ie.Visible = True
Do
Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
If objItem.DayOfWeek = 1 Then
day_name = "Monday"
Else If objItem.DayOfWeek = 2 Then
day_name = "Tuesday"
Else If objItem.DayOfWeek = 3 Then
day_name = "Wednesday"
Else If objItem.DayOfWeek = 4 Then
day_name = "Thursday"
Else If objItem.DayOfWeek = 5 Then
day_name = "Friday"
Else If objItem.DayOfWeek = 6 Then
day_name = "Saturday"
Else
day_name = "Sunday"
End If
For Each objItem In colItems
ie.document.getElementById("msg").innerText = "Date:" & vbCrLf & day_name & vbCrLf & objItem.Day & "/" & objItem.Month & "/" & objItem.Year & vbCrLf & vbCrLf & "Time:" & vbCrLf & objItem.Hour & " : " & objItem.Minute & " : " & objItem.Second
Next
Loop
While End
Upvotes: 0
Views: 126
Reputation: 16682
The issue is you have an extra Wend
on your outer While
statement so the While End
expects a Do
(While End
is not valid syntax in VBScript).
Remove Wend
from this line;
While ie.ReadyState <> 4 : WScript.Sleep 100
and change While End
to Wend
.
Once that issue is fixed you will have another problem as your inner Do
statement loop has no way to exit and will continue infinitely unless you supply a condition that will allow it to exit either by specifying While
or Until
or by using Exit Do
.
This might not be what you want but it will stop that specific error. The problem is without guessing what your intention was it is difficult to refactor the code without changing your intent (but it looks like @Hackoo has done it). Would recommend you take a look at the links provided to the VBScript documentation that details how the While
and Do
loops should be used.
Upvotes: 2
Reputation: 18837
Note : You have some syntax mistakes here :
You should write ElseIf
instead of Else If
And in the last line While End
to Wend
In your question you want to continuously update the time; so you can do it with Do ..Loop
statement with a Timeout of 1 second inside with Wscript.sleep 1000
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "about:blank"
While IE.ReadyState <> 4 : WScript.Sleep 100 : Wend
On Error Resume Next
IE.ToolBar = False
IE.StatusBar = False
IE.Resizable = False
IE.Width = 200
IE.Height = 165
REM Here You can add a title to your window if you like
IE.Document.title = "My Title goes here"
IE.document.body.innerHTML = "<p id='msg'></p>"
Set style = IE.document.CreateStyleSheet
style.AddRule "p", "text-align: center;"
REM --------------------------------------------------------------
REM Just for testing i added those two lines
style.AddRule "p", "font-family:times new roman;font-weight:bold;"
style.AddRule "p", "background-color:lightblue;"
REM --------------------------------------------------------------
IE.Visible = True
Do
Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime")
For Each objItem In colItems
If objItem.DayOfWeek = 1 Then
day_name = "Monday"
ElseIf objItem.DayOfWeek = 2 Then
day_name = "Tuesday"
ElseIf objItem.DayOfWeek = 3 Then
day_name = "Wednesday"
ElseIf objItem.DayOfWeek = 4 Then
day_name = "Thursday"
ElseIf objItem.DayOfWeek = 5 Then
day_name = "Friday"
ElseIf objItem.DayOfWeek = 6 Then
day_name = "Saturday"
Else
day_name = "Sunday"
End If
IE.document.getElementById("msg").innerText = "Date:" & vbCrLf &_
day_name & vbCrLf &_
LPad(objItem.Day,2,"0") & "/" & LPad(objItem.Month,2,"0") & "/" & objItem.Year & vbCrLf & vbCrLf &_
"Time:" & vbCrLf &_
LPad(objItem.Hour,2,"0") & " : " & LPad(objItem.Minute,2,"0") & " : " & LPad(objItem.Second,2,"0")
Next
Wscript.sleep 1000
If Err Then 'user clicked red X (or alt-F4) to close IE window
IE.Quit
Set IE = Nothing
wscript.quit
End if
Loop
'---------------------------------------------------------------------------------------
REM This function will left-pad an input value to the given number of characters
REM using the given padding character without truncating the input value:
REM Source : https://stackoverflow.com/questions/18151811/lpad-with-zeros-in-vbscript
REM Written by Ansgar Wiechers
Function LPad(s, l, c)
Dim n : n = 0
If l > Len(s) Then n = l - Len(s)
LPad = String(n, c) & s
End Function
'---------------------------------------------------------------------------------------
Upvotes: 2