How Can I Automatically Dismiss a Message Box After a Specified Length of Time?

How Can I Automatically Dismiss a Message Box After a Specified Length of Time?

  • Comments 10
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! I’d like to display a Yes/No message box for a few seconds and then, if no one clicks a button, have the message box disappear and the script perform the default operation. How do I do that?

-- JR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JR. Sounds like you need the WSH Shell object and the Popup method. Popup works a lot like the VBScript Msgbox function: like Msgbox it displays various types of graphical message boxes (for example, one with Yes and No buttons; one with OK and Cancel buttons; etc.). There’s one big difference, however. When you display a message box using the Msgbox function, that message box stays on screen until someone clicks a button and dismisses it; if no one clicks a button, the message box will stay there forever. (And, by extension, the script will remain frozen in place.) With the Popup method, however, you can specify an optional timeout value; when the specified amount of time has elapsed, the message box automatically dismisses itself, and the script is free to resume operation.

Let’s take a look at a simple script that displays a message box (one with Yes and No buttons) and then reports whether the user clicked one of these buttons or whether the message box dismissed itself after the 10-second time limit was reached:

Const wshYes = 6
Const wshNo = 7
Const wshYesNoDialog = 4
Const wshQuestionMark = 32

Set objShell = CreateObject("Wscript.Shell")

intReturn = objShell.Popup("Do you want to delete this file?", _
    10, "Delete File", wshYesNoDialog + wshQuestionMark)

If intReturn = wshYes Then
    Wscript.Echo "You clicked the Yes button."
ElseIf intReturn = wshNo Then
    Wscript.Echo "You clicked the No button."
Else
    Wscript.Echo "The popup timed out."
End If

The script begins by defining a few constants. The first two - wshYes and wshNo - are used to determine which button the user clicked. Clicking the Yes button returns 6; clicking the No button returns 7. If the script times out and the popup dismisses itself, then -1 is returned. Later on in the script we’ll check for these values in order to determine which action to take.

The other two constants - wshYesNoDialog and wshQuestionMark - are used to construct the message box; wshYesNoDialog specifies a message box with Yes and No buttons, and wshQuestionMark indicates that the message box should also include a question mark icon. In fact, our finished message box will look an awful lot like this:

Popup Message Box

After defining the constants we create an instance of the WSH Shell object and then call the Popup method (with the return value to be assigned to the variable intReturn). For this script we’ve provided the Popup method with four parameters:

Parameter

Description

Do you want to delete this file?

Message box message.

10

Timeout value, in seconds. If no one clicks a button within 10 seconds, the message box will automatically dismiss itself.

Delete File

Message box title.

wshYesNoDialog + wshQuestionMark

Message box type: a message box with Yes and No buttons and a question mark icon.

When we call the Popup method the message box appears on screen. When the message box is dismissed - either because someone clicked a button or because the message box timed out - the return value is assigned to the variable intReturn. The script then checks the value of intReturn and reports whether the Yes button was clicked, whether the No button was clicked, or whether the message box timed out.

Hopefully this will do the trick for you. For a slightly more useful script, here’s one that asks whether or not you want to delete a file (C:\Scripts\Test.vbs). If you click No, the script terminates and the file is left untouched. However, if you click Yes or if the message box times out, the file is deleted.

Here’s the script:

Const wshYes = 6
Const wshNo = 7
Const wshYesNoDialog = 4
Const wshQuestionMark = 32

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

intReturn = objShell.Popup("Do you want to delete this file?", _
    10, "Delete File", wshYesNoDialog + wshQuestionMark)

If intReturn = wshNo Then
    Wscript.Quit
End If

objFSO.DeleteFile("c:\scripts\test.vbs")

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • But unlike msgbox, objShell.popup isn't "always on top", and so all I get is a blinking notice on my taskbar, and I can't see the popup. Or, in other words, "it ain't modal."

    Now what, Scripting Guy?

  • I can get the popup to time out after 1 second, but no higher.

    Is it an XP thing or did M$ mess it up with an update?

  • Nice script and good thing is it is working here on my pc running xp and opening new window on many uses of it :)

  • system modal works like msgbox

    There is two nType values missing here: Application modal and System modal.

    Those are identical to msgbox function, 0 for application modal and 4096 for system modal.

    So the example from above would be:

    intReturn = objShell.Popup("Do you want to delete this file?", _

       10, "Delete File", wshYesNoDialog + wshQuestionMark + 4096)

    This will pop up on top of whatever you're doing and take focus.

  • @Just a coda great point. One thing to keep in mind, is that in VBScript, wshYesNoDialog  is not a defined constant. In VBScript it si VBYesNo, and VBQuestion. These are documented on MSDN msdn.microsoft.com/.../t1ft6yf3(VS.85).aspx

  • I want to close the pop up box automatically without performing any action.

    I neeed a shell script for this. That should work in win 2000

    Kindly do the need..

  • Or You could just do this!

    Wscript.timeout = 1

    MsgBox "Custom Text",vbCritical,"Custom Title"

  • @David Ur right but u forgot the part ppl will forget to do.

    Open a new notepad file and then enter the text

    Wscript.timeout = 1

    MsgBox "Custom Text",vbCritical,"Custom Title"

    Then Save As: Name.vbs

  • ' This will show the message SMS Sent and automatically close after 1 second
    a = CreateObject("WScript.Shell").Popup("SMS Send", 1, "Shree Softwares")
    Unload Me