# How do I play a .wav file in a vbscript?

**URL:** <https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556>\
**Category:** Factual Questions\
**Created:** [June 10, 2004, 12:06am UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556 "2004-06-10T00:06:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ready29003](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@ready29003](https://boards.straightdope.com/u/ready29003)\
**Post date:** [June 10, 2004, 12:06am UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556/1 "2004-06-10T00:06:45Z")

</div>

I have my vbscript working…now I just need it to play a wav file.

How do I do that? I have search and searched. Instead of “A Job is printing” I want it to play a .wav file. Please help. thanks.

Here is my script:

WARNING\_THRESHOLD=0  
Const PAUSETIME = 500  
For i = 1 to 120000  
strComputer = “.”  
Set objWMIService = GetObject(“winmgmts:” \_  
& “{impersonationLevel=impersonate}!\” & strComputer & “\root\cimv2”)  
Set colPrintQueues = objWMIService.ExecQuery \_  
(“Select \* from Win32\_PerfFormattedData\_Spooler\_PrintQueue Where Name \<\> ‘\_Total’”)  
For Each objPrintQueue in colPrintQueues  
If objPrintQueue.Jobs \> WARNING\_THRESHOLD Then  
Wscript.Echo objPrintQueue.Name & “A Job is Printing!”  
End If  
NEXT  
Wscript.Sleep PAUSETIME  
Next

---

<div class="post-metadata">

**Author:** ![qshapadooy](https://avatars.discourse-cdn.com/v4/letter/q/c67d28/32.png) [@qshapadooy](https://boards.straightdope.com/u/qshapadooy)\
**Post date:** [June 10, 2004, 12:23am UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556/2 "2004-06-10T00:23:23Z")

</div>

```auto

strWaveFile="C:\WINDOWS\Media\ding.wav"

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "sndrec32 /play /close """ & strWaveFile & """",0,True
Set objShell = Nothing

```

---

<div class="post-metadata">

**Author:** ![ready29003](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@ready29003](https://boards.straightdope.com/u/ready29003)\
**Post date:** [June 10, 2004, 2:31am UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556/3 "2004-06-10T02:31:01Z")

</div>

Hey that works perfectly!

Just out of curiousity, is it possible to have VBscript tell the PC internal speaker to beep? Just wondering.

Matt

---

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [June 10, 2004, 4:58am UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556/4 "2004-06-10T04:58:34Z")

</div>

> [@ready29003](#):
>
> Hey that works perfectly!
> 
> Just out of curiousity, is it possible to have VBscript tell the PC internal speaker to beep? Just wondering.
> 
> Matt

```auto

Set oShell = CreateObject("Wscript.Shell")

Beep 1, 0

Sub Beep (n, wait)

For i = 1 To n
     oShell.Run "%comspec% /c echo " & Chr(7), 0, False
     Wscript.Sleep wait*100
     Next
End Sub

```

---

<div class="post-metadata">

**Author:** ![ready29003](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@ready29003](https://boards.straightdope.com/u/ready29003)\
**Post date:** [June 15, 2004, 3:13am UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556/5 "2004-06-15T03:13:53Z")

</div>

Ok, great. I can play a wav file in my script.

Now they tell me they want the wav file to keep playing until someone at the computer tells it to stop.

Can that be done?

Thanks!

Matt

---

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [June 15, 2004, 2:23pm UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556/6 "2004-06-15T14:23:13Z")

</div>

> [@ready29003](#):
>
> Ok, great. I can play a wav file in my script.
> 
> Now they tell me they want the wav file to keep playing until someone at the computer tells it to stop.
> 
> Can that be done?
> 
> Thanks!
> 
> Matt

Not a VBScript method but…

You can try going a whole different route and using an HTML embed. Loop = “true” will make the sound play over and over.

\<embed src=“your\_wave\_file.wav” autostart=“true” hidden=“true” loop=“true”\>

You can then use JavaScript to allow the user to control the sound:  
[http://developer.irt.org/script/sound.htm](http://developer.irt.org/script/sound.htm)

You might find it difficult though to make this work in all browsers.

---

<div class="post-metadata">

**Author:** ![ready29003](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@ready29003](https://boards.straightdope.com/u/ready29003)\
**Post date:** [June 15, 2004, 6:09pm UTC](https://boards.straightdope.com/t/how-do-i-play-a-wav-file-in-a-vbscript/249556/7 "2004-06-15T18:09:31Z")

</div>

```auto

Option Explicit

Dim strComputer, strTargetPrinter, strWaveFile, i, j, LoopVariable
Dim objWMIService, objPrintQueue, colPrintQueues, objShell
Const PAUSETIME = 500
strTargetPrinter = "MLOCAL2"
strWaveFile = "c:\statlabelsoundscript\buzzer.wav"
LoopVariable = 1
Do Until LoopVariable = 0

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrintQueues = objWMIService.ExecQuery _
    ("Select * from Win32_PerfFormattedData_Spooler_PrintQueue Where Name = '" & strTargetPrinter & "'")

For Each objPrintQueue in colPrintQueues
    If objPrintQueue.Jobs <> "0" Then
        Set objShell = CreateObject("Wscript.Shell")
        objShell.Run "sndrec32 /play /close """ & strWaveFile & """",0,True
        Set objShell = Nothing
    End if
Next
Wscript.Sleep PAUSETIME
LOOP

```

Any other ideas for how to make it play until someone hits OK or something?
