How Can I Display a Message Box That Has No Buttons, and That Disappears After a Specified Period of Time?
Hey, Scripting Guy! I would like to display a message box for a set amount of time and then have it disappear. However, I’d prefer that this message box not have a close button, or have anything else that would allow the user to close the message, minimize the message, etc. How can I do that?
Hey, D. You know, this question takes us back to the beginning days of Hey, Scripting Guy! Back when we first started, it seemed like each and every column would go like this:
Hey, Scripting Guy! How can I do x in a script?
Hey, SP. You can’t.
But, wait, don’t go yet: we have a workaround for you. All you have to do is ….
The first time we told people they couldn’t do something and then immediately turned around and told them how to do it, well, that was pretty cute; by the 138th time, hwoever, it wasn’t quite so cute anymore. Because of that we swore on Peter’s grave that we would never use such a corny opener ever again.
Which didn’t make Peter all that happy, considering the fact that he’s still very much alive. In our defense, though, with Peter it’s sometimes hard to tell.
So instead of saying that you can’t display a message box that doesn’t have a button on it (and then – after dashing your hopes – telling you that we have a workaround anyway) we’ll just talk about something else. You know… we’ll talk about … stuff. Right. Oh: baseball. What’s up with that? And, that one thing, the one that was in the newspaper? That was really something, wasn’t ….
Oh, what the heck: the truth is, D, you can’t display a message box (for any amount of time) that doesn’t include a button of some kind. But, wait, don’t go yet: we have a workaround for you.
Note. Sorry. But that just had to be said.
So what kind of workaround do we have? This kind:
<html> <HTA:APPLICATION Caption=”no” >
<script language = “VBScript”> Sub Window_OnLoad idTimer = window.setTimeout(“PausedSection”, 5000, “VBScript”) End Sub
Sub PausedSection window.close End Sub </script>
<body> <p>Your message goes here</p> </body> </html>
What we’ve got here is a simple little HTA (HTML Application). An HTA is similar to an HTML file; the big difference is that HTAs use the file extension .HTA and are designed to run locally instead of over the Internet. In addition, you can create an HTA that does not include a close button (or even a title bar), and you can display any message you choose to display. Technically it’s not a message box, but we’re assuming that doesn’t matter: you just want something to display on screen for a set amount of time and then disappear. On top of that, you don’t want to give the user an obvious way to dismiss that message before the time expires. The little HTA we’re about to show you meets all those criteria.
Note. Where can you learn more about HTAs? Funny you should ask ….
So how do we get this all to work? Well, to begin with, copy the preceding code, paste it into Notepad or some other script editor, and then save the file with a .HTA file extension. First things first.
Now let’s talk about the file you just created. The HTA itself is divided into three sections: basic properties for the HTA, the message displayed in the body of the HTA, and the code that causes the HTA to dismiss itself after a set amount of time (in this example, after 5 seconds). Let’s start off by looking at the section where we configure the HTA properties:
<HTA:APPLICATION Caption=”no” >
As you can see, we set only one property here: we set the Caption property to No. What that does is remove the title bar, the close button, the minimize and maximize buttons, and just about everything else from the HTA window, leaving you with something that looks like this:
Nice, huh? (You’re right, it does look a lot like the list of Scripting Guy achievements over the past year or so, doesn’t it?)
If that’s a bit too severe for you, then use this code and set the SysMenu property to No:
<HTA:APPLICATION SysMenu=”no” >
When you do that you’ll still have the title bar, but no close, minimize, or maximize buttons, so there’s still no obvious way to close the HTA. (To dismiss it you’d need to terminate the Mshta.exe process.)
If you go this route you might also want to use the <TITLE> tag to give the page a title:
<head> <title>Test</title> </head>
Just insert that right under the <HTML> tag. That way you’ll have a custom caption (in this case, Test) that appears in the title bar. If you don’t specifically assign a title then the file path will appear in the title bar.
If you thought it was easy to configure the HTA properties, well, have we got news for you: it’s even easier to specify a message for your HTA:
<body> <p>Your message goes here</p> </body>
Just type your message in the space labeled Your message goes here, and you’re on your way. And because this is HTML you can change fonts and font sizes, add graphics, and do anything else that can be done in HTML. But we’ll leave that up to you. (Hey, it wouldn’t be any fun for you if we did everything, now would it?)
That brings us to the <SCRIPT> section, which is where we control the amount of time that the HTA appears onscreen. To do that, we use these two subroutines:
Sub Window_OnLoad idTimer = window.setTimeout(“PausedSection”, 5000, “VBScript”) End Sub
Sub PausedSection window.close End Sub
You’re right: there isn’t much happening here, is there? To begin with, we have a subroutine named Window_OnLoad; as you probably know, the Window_OnLoad subroutine is designed to automatically run any time an HTA is loaded or refreshed. As you can see, we don’t do much inside this subroutine; in fact, all that we do do is create a timer named idTimer. That timer is designed to wait a specified number of seconds and then call a second subroutine (PausedSection). What will this second subroutine do? You’ll find out in just a second.
Here’s what the code looks like:
idTimer = window.setTimeout(“PausedSection”, 5000, “VBScript”)
As it turns out, to create a timer all we have to do is call the setTimeout method, passing three parameters:
PausedSection, the subroutine we want to call after the five-second pause.
5000, the number of milliseconds to pause. As you probably figured out, 1,000 milliseconds is equal to one second. Want to display the message for 20 seconds? No problem; just change this parameter to 20000.
VBScript, the script language we’re using.
In other words, each time we load the HTA the Window_OnLoad subroutine sets a timer. That timer will click off 5,000 milliseconds and then run a subroutine name PausedSection. In turn, that subroutine does one thing and one thing only; it closes the HTA:
Sub PausedSection window.close End Sub
The net result? An “unclosable” HTA will pop up on screen, display your message, and then automatically disappear after 5 seconds. Which sounds like exactly the sort of behavior you were hoping to get.
Note. Good point: by default the HTA window is a little big, and isn’t centered on the screen, either. Fortunately, we have a column here and another column here that can help you resolve those issues.
After you’ve created the .HTA file all you have to do within your script (that is, within the .vbs file) is include code that will call and display this HTA. Something along these lines ought to do the trick:
Set objShell = CreateObject(“Wscript.Shell”) objShell.Run “no_button.hta”
We hope that helps, D. Granted, it’s not exactly what you asked for; for one thing, it’s not a message box. But seeing as how you can’t display a message box that doesn’t have any buttons, well, this seems like the next best thing.
And, yes, some people would call this a workaround. But the Scripting Guys would never do that.