How Can I Play a Sound From Within a Script?


Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script that pops up a message box to alert users to a problem. Is there any way I can play a sound at the same time the message box appears?

— TL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TL. You guys do this on purpose, don’t you; you love to ask questions where there isn’t a clear-cut answer. This is yet another case of that: you can search high and low through the VBScript and the WSH documentation, but you won’t find any sort of method that allows you to directly play a sound from within a script, not so much as a peep. Or beep, as the case may be.

Because of that, you’ll have to call another utility in order to play a sound. As long as your sound is in the .WAV format, your best bet is to use the Windows Sound Recorder. There are at least two reasons for that. For one thing, the Sound Recorder is lightweight and reasonably quick to load; Media Player is far more powerful, but is probably overkill just to play a beep. For another, by passing the appropriate command-line parameters, you can run Sound Recorder invisibly; as a result, you (or the user) will hear the sound, but won’t see Sound Recorder onscreen.

Here’s a script that will play Notify.wav, a standard operating system sound found in the Windows\Media folder:

strSoundFile = “C:\Windows\Media\Notify.wav”
Set objShell = CreateObject(“Wscript.Shell”)
strCommand = “sndrec32 /play /close ” & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True

The first two lines of the script are pretty simple. In the first line, we just assign the file path to the variable strSoundFile; in the second line, we create an instance of the Wscript Shell object, the object we’ll use to actually run the Sound Recorder.

Things get a tiny bit trickier in line 3. To run Sound Recorder from the command-prompt, we need to type in this command:

sndrec32 /play /close “C:\Windows\Media\Notify.wav”

The first two command-line parameters tell Sound Recorder to play the file and then terminate itself; the third parameter, of course, is the name of the file we want to play. In this example, we don’t need to enclose the file path in double quotes; that’s only required if there are spaces in the path name. We went ahead and enclosed it in double quotes to give you a template you can use when playing a sound file that does have a space in the path name. Thus:

sndrec32 /play /close “C:\Windows\Media\Windows XP Error.wav”

In order to enclose the file path in double quotes, we use the Chr(34) command, which, well, inserts a double quote in our string. Hence this line of code, which strings together sndrec32 /play /close, a double quote mark () the file C:\Windows\Media\Notify.wav, and another double quote mark ():

strCommand = “sndrec32 /play /close ” & chr(34) & strSoundFile & chr(34)

Finally, in line 4 we use the Run method, passing along the command string we just constructed. The 0 parameter causes Sound Recorder to run in a hidden window; the True tells the script to wait until the sound finishes playing before resuming.

As for playing a sound at the same time a message box appears, well, you’ll find that close-to-impossible; that’s because it takes a second or two for the Sound Recorder to load and start playing. Your best bet is to do two things. First, use the False parameter when calling the Sound Recorder; that will tell the script to start the recorder and then continue on, without waiting until the sound finishes playing.

Second, use the Wscript.Sleep command to pause the script for a second or so; that will give the Sound Recorder time to load and – with luck – the sound will start playing right about the time the message box appears. Will it start playing at exactly the same time the message box appears? Probably not. But at least you can come close.

Here’s a sample script that plays a sound and – at about the same time – pops up a message box as well:

strSoundFile = “C:\windows\Media\Notify.wav”
Set objShell = CreateObject(“Wscript.Shell”)
strCommand = “sndrec32 /play /close ” & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, False
Wscript.Sleep 1000
Msgbox “A problem has occurred.”


Discussion is closed.

Feedback usabilla icon