How Can I Play a Sound Each Time a Message Box is Displayed?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I was using a script written by one of my colleagues, and I noticed that any time this script displayed a message box a sound was played as well. How can I play a sound each time I display a message box?

— KI

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KI. You’ll have to give us a second here; the Scripting Guy who writes this column got home a little late last night, having ventured into the wilds of Seattle to watch the Mariners play the Angels. (And yes, now that the Scripting Son’s baseball season is finally over, he and his father celebrated by, uh, going to a baseball game.) As it turned out, however, the two saw a pretty good game: not only did the Mariners win 2-0, but the Scripting Guy who writes this column correctly predicted that the green hydroplane would win the animated Hydro Challenge. Not a bad evening no matter how you look at it.

In addition, father and son also had the pleasure of sitting in front of a guy who knew everything about baseball, and who kept up a non-stop commentary all night long. To give you a sample of what the night was like, here are a few of his more interesting pronouncements:

“Richie Sexson, now there’s a guy who was really hurt by the crackdown on steroids. He had that one year where he hit 75 home runs, but after the crackdown, nothing. Not even close.”

Note. In case you’re wondering, Richie Sexson has never hit 75 home runs in a season; in fact, the record for most home runs in a season (held by Barry Bonds) is 73. The best Richie Sexson ever did was hit 45 homers in one year, something he accomplished twice while playing for the Milwaukee Brewers.

“Hank Aaron was the only clean baseball player, ever. Everyone before him and after him was all drugged-up. They’re all a bunch of liars, cheats, and thieves.”

“Oh, sure, baseball executives, politicians, businessmen – they all have to pretend that the problem never existed. If they owned up to the fact that the players have all been taking steroids then they’d have to give all the fans their money back, and they’d have to refund all the money paid out for advertising and TV rights and everything else. That would wreck the entire economy, absolutely wreck it. Because of that, everyone pretends that there’s no such thing as steroids. That’s why they’ve never cracked down on any of this stuff.”

Note. For an interesting comparison, see the first quote, the one about baseball having cracked down on steroids.

And, of course, our very favorite:

“Come on, Beltre; hit the ball! You know what? I think Adrian Beltre needs Viagra for his batting brain!”

No, we have no idea what that means, either. Send your suggestions to scripter@microsoft.com (in English, if possible).

On second thought, don’t bother; all things considered, we’d rather not know what that means.

In all fairness, however, we must admit that the guy did say at least one thing that made a lot of sense:

“Come on, blue: open your eyes! Don’t you know that you can play a sound any time a message box appears simply by including an icon in that message box?”

In other words:

Msgbox “An error has occurred. Do you want to continue?”, _
    vbYesNo + vbCritical, “Path Not Found”

So did this guy really know what he was talking about? Well, let’s find out. As you can see, in this example we’ve simply called the Msgbox function and passed in three parameters:

“An error has occurred. Do you want to continue?”

vbYesNo + vbCritical

“Path Not Found”

The first parameter is easy to figure out; that’s simply the message we want displayed in the message box. The third parameter is also pretty straightforward: that’s the title we want to assign to our message box. That leaves us with this: vbYesNo + vbCritical.

As it turns out, we’re actually defining two things here. First, we’re using the VBScript constant vbYesNo to indicate that we want to include a Yes button and a No button in our message box. Are we limited to just Yes and No buttons in a message box? Of course not; here are all the button combinations available to you:

Constant

Buttons Displayed

vbOKOnly

OK

vbOKCancel

OK and Cancel

vbAbortRetryIgnore

Abort, Retry, and Ignore

vbYesNoCancel

Yes, No, and Cancel

vbYesNo

Yes and No

vbRetryCancel

Retry and Cancel

But notice that we aren’t just defining the set of buttons to be displayed in our message box; we’re also defining an icon to be displayed. That’s what the constant vbCritical is for; it displays a Critical Message icon (a big red circle with an X in it) in the message box. You know, like this:

Message Box


Ah, but that’s not all. When you assign an icon you get the icon, sure, but you also get the system sound associated with the event (in this case, Critical Message). That’s how you can play a sound each time a message box is displayed: just be sure and assign an icon to the message box. Don’t like either the sound or the icon for Critical Message? Then try one of these constants instead:

Constant

Icon Displayed/Sound Played

vbCritical

Critical Message

vbQuestion

Warning Query

vbExclamation

Warning Message

vbInformation

Information Message

See how this works? Suppose you wanted to display OK and Cancel buttons along with the Warning Message icon (and sound). That’s fine; in that case your call to the Msgbox function would look like this:

Msgbox “An error has occurred. Do you want to continue?”, _
    vbOKCancel + vbExclamation, “Path Not Found”

Etc.

Here’s another nifty trick. Try adding the constant vbSystemModal to the Msgbox function:

Msgbox “An error has occurred. Do you want to continue?”, _
    vbOKCancel + vbExclamation + vbSystemModal, “Path Not Found”

Why would you want to do that? Well, if you make this a system modal message box, then the message box remains on top of all your other windows, even if you switch to a different application. (You can still use that application, but the message box remains on top. Give it a try and you’ll see what we mean.) Is that useful? It can be; if nothing else, it makes it a little bit harder for users to ignore your message box.

Before you ask, yes, you are limited to those four icons and the system sounds associated with those events. (And again, before you ask, there’s really no way to change those system sounds programmatically.) If you’d rather play a different sound each time a message box is displayed then you might want to take a look at one of our previous Hey, Scripting Guy! columns. The method employed there is far from foolproof, but you might find it useful. Give it a try and see for yourself.

That should do it, KI. As for the baseball game, we’ll let the Scripting Son have the last word. Mystified by a pair of ball and strike calls, the Scripting Son sighed in exasperation. “Come on, blue,” he called to the ump. “Try turning home plate over and reading the instructions.” Although the Scripting Guy who writes this column has hung around baseball fields all his life, he’d never heard that one before.

0 comments

Discussion is closed.

Feedback usabilla icon