John Howard - Senior Program Manager in the Hyper-V team at Microsoft

Senior Program Manager, Hyper-V team, Windows Core Operating System Division.


How to automatically dismiss scripted dialogs boxes - alternative to msgbox function

  • Comments 4
  • Likes

I guess I've been doing a fair bit of scripting recently - admittedly nowhere near as much as I did back in my dev days, but there's always something you learn new. I don't even have a specific use for this, neither was I able to imagine a practical situation I could find for its' potential use it, but who knows who's reading this....?

Generally, in a VBScript I would have used the MsgBox function to display a dialog. Although I've also used the WScript.Shell object a fair few times, I've never had need to use the "Popup" method to also display a dialog when MsgBox was to hand without instantiating another object if you didn't need it.

However, purely by accident I was looking at the documentation for WScript.Shell and found that "Popup" can do one thing "MsgBox" can't - it can be dismissed automatically by a timer. Here's a quick code example which shows how it works. Cut and paste into "dialogs.vbs" and run it. Two identical dialogs will be displayed, it's just that the second one will dismiss after 2 seconds.

Dim szTitle        ' Title for dialog
Dim szMessage      ' Message to go in dialog
Dim iFeatures      ' What options on dialog
Dim iSeconds       ' How long to display for :-)
Dim oShell         ' For Popup Method

szTitle = "Sample Title"
szMessage = "Do you know you could do this?"
iFeatures = vbOKCancel or vbQuestion
iSeconds = 2
set oShell = CreateObject("Wscript.Shell")

msgbox szMessage, iFeatures, szTitle
oShell.Popup szMessage, iSeconds , szTitle, iFeatures

BTW - setting iSeconds to 0, or running "oShell.Popup szMessage,,szTitle,iFeatures" will produce identical functionality to msgbox. Even if you too can't see a use, it's one of those things to keep in your back-pocket for your next geeks outing under the banner of "Bet you didn't know how to...."

Here's the links to the full documentation for msgbox and popup.

  • Geek!

  • John has been nagging me to blog about this as he's from an Exchange developer background and gets excited...

  • Actually this could be quite useful when a script will be run both by a user (interactive) and via an automated process. How many times have you noticed a scheduled task still running hours later because the task threw up an unanswered msgbox?

  • Well, there is another difference between the msgbox function and popup method as was pointed out to...

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment