I have this VBScript that doesn't work. When I press "Yes" on the pop-up it says error. Please help. Thanks!
Code:
result = MsgBox ("Yes or No?", vbYesNo, "Start Web")
Select Case result
Case vbYes
shell.CurrentDirectory = "My Directory"
shell.Run "startweb.bat"
Case vbNo
End Select
When I Press "Yes" A .Bat file called: startweb
When I press "Yes" on the pop-up it says error.
What error?
Microsoft VBScript runtime error: Object required, or
The system cannot find the file specified, or
something else?
Solution:
Case ˙Object required`:
Set Shell = WScript.CreateObject("WScript.Shell") ''' add this line
result = MsgBox ("Yes or No?", vbYesNo, "Start Web")
Select Case result
Case vbYes
shell.CurrentDirectory = "My Directory"
shell.Run "startweb.bat"
Case vbNo
End Select
Case file missing: use fully qualified path to "My Directory", e.g. "c:\tests\My Directory"
Case Else: unsolvable; edit your question and add more info.
Related
im writing a code in vbscript where it will ask the user for input and then run certain files according to the input and i have the else so that it will redo the if else sequence when you type something that isnt an option but when i try to press cancel or the red 'X' it acts as if i have put in an invalid input and goes over the else sequence.
Dim sInput
sInput = InputBox("input")
If sInput = "input1" or sInput = "input2" Then
set shell=createobject("wscript.shell")
shell.run "file.bat"
elseif sInput = "exit" or sInput = "Exit" Then
WScript.Quit
else
name=msgbox (" That is not a valid response",0+16,"ERROR")
set shell=createobject("wscript.shell")
shell.run "input.vbs"
end if
Don't try to restart the script.
Use a loop instead. End the loop when the user entered a valid option, or quit the entire program if requested.
Option Explicit
Dim Shell, input, button
Set Shell = CreateObject("WScript.Shell")
Do
input = InputBox("input")
If IsEmpty(input) Or LCase(input) = "exit" Then WScript.Quit
input = LCase(Trim(input))
If input = "input1" Or input = "input2" Then
Shell.Run "file.bat"
Exit Do
Else
button = MsgBox("That is not a valid response.", vbExclamation + vbRetryCancel, "ERROR")
If button = vbCancel Then Exit Do
End If
Loop
Notes:
Option Explicit makes variable declaration mandatory. It's a good idea to always have this enabled.
IsEmpty() is true when the user pressed the Cancel button (or the Esc key) in the InputBox - but this will work only before the response is manipulated in any way, such as LCase or Trim. Supporting the Cancel button is more intuitive than having a special "exit" keyword, so maybe you should get rid of that.
The various constants you can use with MsgBox are described on ss64.com and in more detal in the official VBScript language reference.
You can change what Enter and Esc do in each MsgBox by using the vbDefaultButton1 or vbDefaultButton2 constants.
The Do loop without any conditions (Do/Loop While ... or Do/Loop Until ...) will run forever - be sure not to forget using Exit Do or WScript.Quit(). (If you do, killing the Script with the Task Manager will get you out of it.)
This code is supposed to open a script to simulate a timer when "Yes" is pressed or open a script to simulate an alarm when "No" is pressed. When "Yes" is pressed it works as intended, but when "No" is pressed, it still opens the timer script and not the alarm one.
Pick = MsgBox("Do you want a Timer?", 4+32, "Choose one")
If vbYes Then
CreateObject("WScript.Shell").Run("""C:\Users\Username\Documents\Applications\Reminder\Reminder Script.vbs""")
WScript.Quit
Else
CreateObject("WScript.Shell").Run("""C:\Users\Username\Documents\Applications\Alarm\Alarm Script.vbs""")
WScript.Quit
End If
AFAIK you can do this two ways (See below - please excuse the syntax). The If...Then...Else method is answered by GSerg and pinkfloydx33 in the comments to your question...
set wshell = createobject("wscript.shell")
'**If...Then...Else**
pick = msgbox("Do you want a timer?", 4+32, "Choose one")
if pick = vbyes then
'wshell.run("""c:\users\username\documents\applications\reminder\reminder script.vbs""")
wscript.echo "Reminder"
else
'whsell.run("""c:\users\username\documents\applications\alarm\alarm script.vbs""")
wscript.echo "Alarm"
end if
'**Select...Case**
pick = msgbox("Do you want a timer?", 4+32, "Choose one")
select case pick
case vbyes
'wshell.run("""c:\users\username\documents\applications\reminder\reminder script.vbs""")
wscript.echo "Reminder"
wscript.quit
case vbno
'whsell.run("""c:\users\username\documents\applications\alarm\alarm script.vbs""")
wscript.echo "Alarm"
wscript.quit
end select
Hopefully this helps.
Furthermore the MSDN VBScript User guide may help better explain variables etc.
Hard to make a concise title, but basically, I have started an instance of command prompt from VBS to run an exe, everything works great and I verify my feedback with msgboxes of output line. When the command prompt gets to a part that is loading and says Verifying File (XX%), the VBS does not run anymore. It does not crash, it simply never moves on from its line. I even have noticed that if I don't constantly Writeline, it will pause before that. So while I wait, I constantly write a 1. I dont see anywhere it could be in an infinite loop without showing me a messagebox.
Please help.
set shell = WScript.CreateObject("WScript.Shell")
set oExec = Shell.exec("cmd.exe")
do while Not oExec.StdOut.AtEndOfStream
junkChar = oExec.stdOut.Read(1)
message = message & junkChar
'errormsg = oExec.stderr.readline
if asc(junkChar) = 13 and message <> junkChar then
msgbox message '& len(message)
message = ""
end if
if right(message,1) = ">" and not bool1stCmd then
'msgbox(message)
msgbox("Command" & cmdArchive)
oExec.stdIn.Writeline cmdArchive
bool1stCmd = True
'oExec.StdIn.Write VbCrLf
elseif bool1stCmd then
if InStr(1,message,"Enter a command>")>0 then
msgbox "Enter a command!"
end if
oExec.stdIn.writeline "1"
end if
msgbox "Loop again!"
Loop
msgbox "exiting loop"
I'm new to scripting and am taking a class wherein I have been tasked to create a script that places a shortcut on a user's desktop that shuts down the computer. When the shortcut icon is double-clicked the user is asked if they really want to shut down, and given the option to proceed or cancel.
So here's my problem. The script will create the file that the shortcut needs to be linked to, but it won't actually place a shortcut for it on the desktop. However, if I run just the "create shortcut" script by itself, the shortcut is created correctly and everything runs. I don't know what I'm doing wrong and I can't find anything in my text book to help me. I've been working on this for days. I've attached the script. Thanks for your help!
Option Explicit
On Error Resume Next
Dim fsoObject, open_File, target_File, wshObject, myShortcut
set fsoObject = WScript.CreateObject("Scripting.FileSystemObject")
target_File = "C:\Scripts\ShutdownShortcut.vbs"
Open_Shortcut_File()
open_File.WriteLine "Option Explicit"
open_File.WriteLine "On Error Resume Next"
open_File.WriteLine "Dim shellApp, result"
open_File.WriteLine "Set shellApp = CreateObject(""Shell.Application"")"
open_File.WriteLine "result = MsgBox (""Do you wish to shut down your computer?"", vbYesNo)"
open_File.WriteLine "Select Case result"
open_File.WriteLine " Case vbYes"
open_File.WriteLine " MsgBox(""The system will now shut down ..."")"
open_File.WriteLine " Dim objShell"
open_File.WriteLine " Set objShell = WScript.CreateObject(""WScript.Shell"")"
open_File.WriteLine " objShell.Run ""C:WINDOWS\system32\shutdown.exe -r -t 10"""
open_File.WriteLine " Case vbNo"
open_File.WriteLine " MsgBox(""Shutdown cancelled."")"
open_File.WriteLine "End Select"
Close_Shortcut_File()
Function Open_Shortcut_File()
Set open_File = fsoObject.OpenTextFile(target_File, 2, "True")
End Function
Function Close_Shortcut_File()
open_File.Close()
End Function
Set wshObject = WScript.CreateObject("WScript.Shell")
desktopFolder = wshObject.SpecialFolders("Desktop")
Set myShortcut = wshObject.CreateShortcut(desktopFolder & "\\Shutdown.lnk")
myShortcut.TargetPath = target_File
myShortcut.Save
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
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.
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.
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