Rem: Playing a .wav File
In WSH scripts, you can use the WshShell object’s Run method to play .wav files in Windows’ Sound Recorder program.
June 13, 2004
I want to use a script to monitor current events on a central monitoring server. When a problem is reported to the central monitoring server, I'd like the script to alert me with an audible alarm. Can I play a .wav file from within a VBScript file run from Windows Script Host's (WSH's) CScript host? If so, how do I script it?
You can play a .wav file; the code in Listing 1 shows how. This code uses the Windows Sound Recorder program to play the Windows XP Exclamation.wav file. The code creates an instance of WSH's WshShell object and calls the object's Run method, which takes three arguments. The first argument is the Sndrec32 program, which launches the Sound Recorder, plays the exclamation.wav file, then closes the program. The second argument is an integer value that specifies the appearance of the program's window. A value of 0 prevents the Sound Recorder window from being displayed. The last argument is a Boolean value that you can use to specify whether the script should wait for the specified command to finish executing before continuing to the next statement. In this case, by setting the value to True, you're telling the script to stop executing until the Sndrec32 program finishes.
You don't need to play a .wav file, however, to make your script provide an audible alert. Instead, you can use the ASCII character code for the bell and the VBScript Chr function, as the code in Listing 2 shows. Listing 2 begins by defining a constant to represent the bell's nonprintable ASCII character code, which is 7. Next, the script enters a For...Next statement in which WSH's WScript Echo method echoes the character returned by the Chr function.
When you use the CScript host to run the code in Listing 2, a bell echoes from your workstation's speaker. If you use the WScript host to run this code, you'll get annoying dialog boxes, each of which will contain a garbage character in lieu of the nonprintable ASCII character. In other words, you must use the CScript host to ensure the ASCII character code works as expected.
About the Author
You May Also Like