1

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.

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.

What is the problem?

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
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
user2662535
  • 13
  • 1
  • 1
  • 3
  • 1
    You have `option explicit` in the middle of code, and [excessive parentheses](http://stackoverflow.com/a/10262247/11683) on calls to messageboxes that are not inteded to return value. – GSerg Aug 07 '13 at 23:26
  • It means I just have to move the option explicit ? Sorry I didn't understand the second advice .. – user2662535 Aug 08 '13 at 00:20
  • 5
    "It doesn't work" isn't a problem description. What does "doesn't work" mean? Does the second `MsgBox` not show? Does the computer not shut down? Please be specific when you ask a question here. If you don't understand why, call your auto mechanic and tell them "My car doesn't work. What's wrong with it, and what will it cost to fix?" and see if you get a meaningful answer without providing more details. – Ken White Aug 08 '13 at 00:28

3 Answers3

5

I have amended your code as per below:

Option Explicit

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

The main issues were that "option explicit" has to be at the top, and as a result the "result" variable then must be declared using the "dim" keyword. The above code works fine when I executed it via the command line.

I also added a timeout of 20, but you can easily change this back to the original value of 0.

Sudhir
  • 1,432
  • 3
  • 15
  • 16
4

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.

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 & "."

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
2

Try This:

Set Shell = CreateObject("WScript.Shell")

     Answer = MsgBox("Do You Want To" & vbNewLine & "Shut Down Your Computer?",vbYesNo,"Shutdown:")
     If Answer = vbYes Then
          Shell.run "shutdown.exe -s -t 60"
          Ending = 1
     ElseIf Answer = vbNo Then
          Stopping = MsgBox("Do You Wish To Quit?",vbYesNo,"Quit:")
          If Stopping = vbYes Then
               WScript.Quit 0
          End If
     End If
Jonco98
  • 392
  • 2
  • 20