I have this script that will display a yes-no box with a 30 second countdown, and if the user has not selected an option after the time is up, then the VBScript automatically selects the option "Yes" and returns to a Batch Script.
How do I remove the countdown part from the script, so that it will not default to "Yes", but instead wait until input is given.
Many Thanks
Option Explicit
Dim oShell, retCode
Set oShell = WScript.CreateObject("WScript.Shell")
retCode = oShell.Popup("Place your question here?", 30, "Title", 4 + 32)
Select Case retCode
case 6, -1
WScript.quit(0) 'Yes or time-out was chosen
case 7
WScript.quit(1) 'No was chosen
End Select
vbscript taken from http://www.msfn.org/board/topic/138818-vbscript-msgbox-with-auto-select-countdown-and-batch-script-input/
This should be the answer, for more reference:
WScript.Shell.Popup
Dim oShell, retCode
Set oShell = WScript.CreateObject("WScript.Shell")
retCode = oShell.Popup("Place your question here?", 0, "Title", 4 + 32)
Select Case retCode
case 6, -1
WScript.quit(0) 'Yes or time-out was chosen
case 7
WScript.quit(1) 'No was chosen
End Select`
Related
This question already has answers here:
How can I show a message box with two buttons?
(8 answers)
Closed 1 year ago.
I was creating a vbs but after i click Yes in msgbox, it instead does what the "No" is supposed to do.
Here is my code:
X=MsgBox("Please Restart. Our system will break down.",4+4096, ErR0r)
if vbNo then
X=MsgBox("e")
dim count
set object = wscript.CreateObject("wscript.shell")
do
object.run "error.vbs"
count = count + 1
loop until count = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
end if
if vbYes then
Dim IntCounter
Dim objWshShl : Set objWshShl = WScript.CreateObject("wscript.shell")
Dim objVoice : Set objVoice = WScript.CreateObject("sapi.spvoice")
ShutdownWarning()
TimedMessageBox()
ShutdownComputer()
Function ShutdownWarning
X=MsgBox("happy late april fools, hehe", 0+4096)
WScript.Sleep 5000
End Function
Function TimedMessageBox
For IntCounter = 5 To 1 Step -1
objWshShl.Popup "Your time will be wasted in " _
& IntCounter & " seconds",1,"Computer Shutdown", 0+4096
Next
End Function
Function ShutdownComputer
objWshShl.Run "Shutdown /s /f /t 0",0
End Function
end if
does anyone have solutions to this?
(im not trying to create a legitimate virus, this is just a prank i made for fun)
vbNo is a constant value equal to 7.
if vbNo will always be true, since 7 is not a falsy value.
You need to use a variable to get the return value of the user input, such as :
userInput = msgBox("click yes or no", vbYesNo)
if userInput = vbNo Then
' do something
end if
I want to clear or automatically close popup windows through vbscript, but they may be different popup windows or may be a popup window within popup window.
I am using a vbscript to create pop a window and I want a script which will automatically close all the popup windows. Below is the script which creates pop up windows and I want to close all these pop up windows through script.
Dim WshShell, BtnCode
Set WshShell = WScript.CreateObject("WScript.Shell")
BtnCode = WshShell.Popup("Do you like your job?", 7, "Answer This Question:", 4 + 32)
Select Case BtnCode
case 6 WScript.Echo "That's great!."
case 7 WScript.Echo "Sorry to hear that."
case -1 WScript.Echo "No Response?"
End Select
My solution to above code where it works partially:
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Answer this question")
If ret = True Then
wshShell.SendKeys "%Y"
Exit Do
End If
WScript.Sleep 500
Loop
I am programming in VBScript and I just got the hang of the ReturnValue = MsgBox("Content", 36, "Title"), and I want my code to close when you press the yes button, but open an application when you press the no button.
Here's the code so far:
ReturnValue = MsgBox("Content", 36, "Title")
If ReturnValue = 7 Then
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.run "open this.exe"
End If
If ReturnValue = 6 Then
WScript.Quit
End If
Your script seems to work fine as is. Here it is with minor changes to make the functionality more obvious:
ReturnValue = MsgBox("Click yes to exit." + vbCrlf + "Click no to open Notepad.", 36, "Exit Script?")
If ReturnValue = 7 Then
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.run "notepad.exe"
End If
If ReturnValue = 6 Then
MsgBox "Script is now exiting"
WScript.Quit
End If
If you are experiencing other behavior, please let us know what it is.
Here is the full documentation for the MsgBox command: http://ss64.com/vb/msgbox.html
I have written a simple script to press ok on message box but its not working. Please help me how to do this
here is the sample code
set oWShell = createobject("WScript.Shell")
MsgBox "Hello"
WScript.Sleep 2000
oWShell.Sendkeys "{enter}"
MsgBox waits for the click. If you don't click yourself, it gets never to "Sleep" or "SendKeys".
I assume you are just trying to learn, because this code makes no sense. If you want to press a button on another programs window, this could work. But in its own process this doesn't work.
If you really want to click your own MsgBox, you have to do it with a separate script. One creates the MsgBox and another clicks the button.
If you just want to close a message box after a certain period of time, check out the Popup() method of the WshShell class. Its second parameter specifies the number of seconds to display the message box for before closing it.
With CreateObject("WScript.Shell")
' Display a message box that disappears after two seconds...
.Popup "Hello", 2
End With
chek this site
you need this:
Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox messageText, 51, "Warning......!"
how to click on button in message box and control of display time here the script can do that .
just copy this lines of code and paste in text file then save it as "ControlMsgBox.vbs".
''' IN THE NAME OF ALLAH
' THIS SCRIPT CONTROL OF MSGBOX DISPLAY TIME
' LET SENKEYS DEAL WITH THE MSGBOX
' SOLVE THE PROBLEM OF APPACTIVATE NOT WORKING EFFECTIVE
On Error Resume Next
Dim Sh : Set Sh=CreateObject("wscript.shell") ' declare and create the wshshell
Dim path : path =Replace(WScript.ScriptFullName,WScript.ScriptName,"") 'declare the variable of the current script path
Dim myMessage : myMessage="This is my message ." 'declare variable of the of the display text of msgbox
Sh.run "cmd.exe /c cd """&path&""" && echo msgbox """&myMessage&""",,""hello"" > mymsgbox.vbs",0,false 'create masgbox script in the same path
WScript.Sleep 1000 'wait 1 sec to let process of create msgbox script execute
Sh.run "mymsgbox.vbs" 'run the created msgbox script
WScript.Sleep 3000 ' let the msgbox display for 3 sec before we sendkeys to close
For i=0 To 600 ' loop to retry select correct msgbox window about 1 min
ret = Sh.AppActivate("In_The_Name_Of_Allah") 'select the activate msgbox window (if this loop 300 this mean loop will continue 30 sec if 600 (1 min)
If ret = True Then ' check if the msgbox windows select or not
Sh.SendKeys "%N" 'send key of Alt+N to select first button in msgbox (ok)
End If
ret = Sh.AppActivate("In_The_Name_Of_Allah") 'recheck again to be sure that we will not send key out of target windows (msgbox window)
If ret = True Then
Sh.SendKeys "{enter}" ' send key to click enter
wscript.sleep 500
ret=Sh.AppActivate("In_The_Name_Of_Allah")
If ret=False Then ' using nested IF to sure of selected windows is false because its close
Exit For ' exit for loop directly and execute what after for next
End If
End If
WScript.Sleep 100
Next
With CreateObject("Scripting.FileSystemObject")
If .FileExists(path&"mymsgbox.vbs") Then 'check if the msgbox script we create form this script exist or not
.DeleteFile(path&"mymsgbox.vbs") 'delete the msgbox script we create after message window closed
End If
End With
Set Sh=Nothing 'remove the sh object create
WScript.Quit ' terminate wscript.exe instance who run this script
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