简体   繁体   English

在 VBScript 中的循环开始时无法识别“Do”

[英]“Do” not recognised at start of loop in VBScript

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.但由于某种原因,系统在循环开始时无法识别Do It is in a while loop, if that changes anything.它在一个while循环中,如果这改变了任何东西。 I also put a On Error Resume Next but still get error message.我还放了一个On Error Resume Next但仍然收到错误消息。 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

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) .问题是您的外部While语句上有一个额外的Wend ,因此While End需要一个Do ( While End在 VBScript 中不是有效的语法)

Remove Wend from this line;从此行中删除Wend

While ie.ReadyState <> 4 : WScript.Sleep 100

and change While End to Wend .并将While End更改为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 .一旦该问题得到解决,您将遇到另一个问题,因为您的内部Do语句循环无法退出并且将无限继续,除非您提供一个条件,允许它通过指定WhileUntil或使用Exit Do

Summary概括

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 ) .问题是没有猜测你的意图是很难重构代码而不改变你的意图(但看起来@Hackoo 已经做到了 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.建议您查看提供给VBScript 文档的链接,该文档详细说明了应如何使用WhileDo循环。

Note: You have some syntax mistakes here: You should write ElseIf instead of Else If注意:这里有一些语法错误:你应该写ElseIf而不是Else If

And in the last line While End to Wend在最后一行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所以你可以用Do..Loop语句来做到这一点,其中 Timeout 为 1 秒, 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
'---------------------------------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM