If Statement for Function 循环不正确

If Statement for Function looping incorrectly

我正在尝试在 VBScript 中重新创建一个我在 Python 中编写过的简单程序,以便在对话框中显示它 运行。

这是代码:

Set objShell = CreateObject("Wscript.Shell")

Function Query()
    query = InputBox("Please input the Subreddit you with to navigate to e.g (globaloffensive)", "iixCarbonxZz's Subreddit Finder")
End Function

Do
    If query = "" Then
        err = MsgBox("Please enter a Valid Subreddit", vbOKOnly + vbExclamation, "Invalid")
    Else
        objShell.Run("http://www.reddit.com/r/" & query)
        WScript.Quit()
    End If
    Query()
Loop

我的问题是:

代码打开输入文本框,如果在按下 OK 按钮时文本框中没有任何内容,它应该弹出 'err' 错误消息框,然后循环。如果它在输入框中得到一些东西,它应该使用它然后关闭脚本。

实际上,如果我将该框留空,它会显示消息,循环回到输入框,但如果再次留空,则会跳过 if 语句并重新加载输入框。如果输入了它可以使用的内容,它会跳过 if 语句并第一次重新加载框,然后 运行s 并第二次关闭。如果在此之前提交了空白,它将在正确使用输入之前再跳过一次 if 语句。

  1. 使用变量存储函数的结果
  2. 不要对变量和函数使用相同的名称

演示:

Option Explicit

Function getQuery()
    getQuery = InputBox("Please input the Subreddit you with to navigate to e.g (globaloffensive)", "iixCarbonxZz's Subreddit Finder")
End Function

Dim query
Do
    query = getQuery()
    If query = "" Then
        err = MsgBox("Please enter a Valid Subreddit", vbOKOnly + vbExclamation,"Invalid")
    Else
        WScript.Echo "http://www.reddit.com/r/" & query
        WScript.Quit 0
    End If
Loop