This is something I found before, and I forgot which website it was from. It has been tested to be functional!
How to play a sound in a script?
Q:
Hi, Scripting Guy! I have a script that alerts the user of a problem by popping up a message box. Is there a way to play a sound at the same time as the message box pops up?
-- TL
A:
Hi, TL. You guys are doing this on purpose, aren't you? You just love asking questions with no clear answers. This time is the same: You can search through VBScript and WSH documentation, but you won't find any way to directly play a sound through the script, not even a "beep" sound. Or, in this case, maybe a "beep-beep" sound.
Therefore, you must call another utility to play the sound. If the sound file is in .WAV format, it's best to use Windows Sound Recorder. There are at least two reasons for this. First, Sound Recorder is relatively small and can be loaded very quickly; Media Player is much more powerful, but it's overkill for just playing a "beep-beep" sound. Second, you can run Sound Recorder implicitly by passing appropriate command-line parameters; so you (or the user) will hear the sound but won't see Sound Recorder on the screen.
The following script can play Notify.wav (a standard operating system sound file 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 this script are very simple. In the first line, we assign the file path to the strSoundFile variable; in the second line, we create an instance of the Wscript Shell object (the object that will be used to actually run Sound Recorder).
The third line is a bit more complicated. To run Sound Recorder from the command prompt, you need to type 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 automatically; of course, the third parameter is the name of the file to play. In this example, you don't need to put double quotes around the file path; you only need to put quotes when the path name contains spaces. We prefixed with quotes to provide you with a template for when you play sound files whose path names do contain spaces. So:
sndrec32 /play /close "C:\Windows\Media\Windows XP Error.wav"
To put double quotes around this file path, we use the Chr(34) command (which inserts a double quote in the string). So this line concatenates sndrec32 /play /close, the opening double quote ("), the file C:\Windows\Media\Notify.wav, and the closing double quote ("):
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
Finally, in the fourth line, the Run method is used, passing the command string that was just built. Parameter 0 makes Sound Recorder run in a hidden window; parameter True tells the script to wait until the sound finishes playing before continuing.
As for playing a sound at the same time as the message box pops up, you'll find that it's almost impossible; this is because Sound Recorder takes a second or two to load and start playing. It's best to do two things. First, when calling Sound Recorder, use parameter False; this will tell the script to start Sound Recorder and not wait until the sound finishes playing to continue.
Second, use the Wscript.Sleep command to pause the script for about one second; this gives Sound Recorder time to load, and hopefully, the sound will start playing around the time the message box pops up. Can it play the sound exactly at the same time as the message box pops up? Usually not. But at least it can be close to simultaneous.
Here's an example script that can also play the sound around the time the message box pops up:
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."
Responsible Editor: Gu Hongxue