是/否关闭 [英] Yes/no shut down

查看:49
本文介绍了是/否关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩 VBScript,我想制作一个 MsgBox,询问用户是否要关闭他们的计算机.

I am playing with VBScript and I want to make a MsgBox which asks the user if they want to shut down their computer or not.

如果用户点击Yes,他们应该首先看到一个 MsgBox,然后他们的计算机开始关闭.

If the user clicks Yes they should see a MsgBox first then their computer starts to shutdown.

我正在使用此代码,但它不起作用.

I am using this code but it doesn't work.

有什么问题?

result = MsgBox ("Shutdown?", vbYesNo, "Yes/No Exm")
Select Case result
    Case vbYes
        MsgBox("shuting down ...")
        Option Explicit
        Dim objShell
        Set objShell = WScript.CreateObject("WScript.Shell")
        objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"
    Case vbNo
        MsgBox("Ok")
End Select

推荐答案

As documented Option Explicit 必须出现在脚本中的任何其他语句之前.在脚本的任何其他地方使用它应该会引发一个预期语句"错误,指向带有 Option Explicit 语句的行.如果您没有收到该错误,则您的代码中有一个 On Error Resume Next 未显示.

As documented Option Explicit must appear before any other statement in a script. Using it anywhere else in a script should raise a "Expected Statement" error pointing to the line with the Option Explicit statement. If you don't get that error, you have an On Error Resume Next in your code that you didn't show.

如果将Option Explicit语句移动到脚本的开头,但仍然没有关闭,则需要检查shutdown的返回值命令:

If you move the Option Explicit statement to the beginning of the script, but the shutdown still doesn't occur, you need to check the return value of the shutdown command:

rc = objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0", 0, True
If rc <> 0 Then MsgBox "shutdown failed with exit code " & rc & "."

MsgBox 语句中的括号应该不会导致问题,只要您只向函数传递一个参数,但我仍然会删除它们.

The parentheses in your MsgBox statements shouldn't cause an issue as long as you pass just a single argument to the function, but I'd still remove them.

这篇关于是/否关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆