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."

Comments (17)

  1. bytehead says:

    sndrec32 does not exist on Windows 7 64-bit.  I expanded SNDREC32.EX_ from my 2003 media.  It has issues doing that.

  2. Anonymous says:

    The 32 in "sndrec32" means it is a 32-bit application. What you have there is a 64-bit operating system. You need to do a little research to see if Windows 7 has an equivalent app. If not, you’re out of luck.

    If I was sitting at my Windows 7 box, I would give you some hints of where to look.

    Good luck!

  3. Anonymous says:

    Hi! Great solution!

    Very often happens I miss important phone calls when I listen to music to loud or have forgotten turn on sound after comming home…

    Original method doesn’t work on my windows8, but I used this SAPI to pair up with Android Notifier plus say it loud, what is happening – call or too low battery…

    Fun coding!

  4. Ram Karthik says:

    Thank You very much – it is working

  5. bqbowden says:

    How do I take this code and use it? It seems it will work for what I need, but I am not familiar with how to take the code and implement it.

  6. Daz, Oxford says:

    There is the SAPI way to play wav file

    s of course:

    '[Tested on Windows XP SP3]

    Set oVoice = CreateObject("SAPI.SpVoice")

    set oSpFileStream = CreateObject("SAPI.SpFileStream")

    oSpFileStream.Open "c:WindowsMediatada.wav"

    oVoice.SpeakStream oSpFileStream


    oVoice.Speak "Finished Playing wave file."

  7. stab says:

    Daz, I like your way better. Is there a way to keep it from telling me it "finished playing wav file"?

  8. stab says:

    Haha. Nevermind, I see it.

  9. EnfoZipper says:

    Cahge this line:

    oVoice.Speak "Finished Playing wave file."


    oVoice.Speak "Don't say finished Playing wave file."

    COmputers are dumba nd have to be told everything.

  10. gggirlgeek says:

    Nope!  The SAPI one didn't work on Windows 7.  Bummer!

  11. Venky says:

    thanks Boss…

    ur code is working on my file…

  12. LEWIS says:

    Thanks, I used this script to play a sound whenever a new visitor arrives in our website and it works like a charm. You guys rock

  13. Andrew Gonzalez says:

    Thanks this helped me ALOT!!!!!!!

  14. ECASE says:


    Script: C:UsersEvanDesktopanything.vbs
    Line: 4
    Char: 1
    Error: The system cannot find the file specified
    Code: 80070002
    Source: (null)

  15. --- says:

    Working for XP; not for else

  16. Moe Zuiter says:

    What "Daz, Oxford" was exactly what I was thinking of, except implemented instead of just another floating thought. I’m using it for many different sounds and it’s flawless. I don’t even know why scripting guy is using that lame method when he could be
    using Daz’s version!

Skip to main content