Playing Wave Files?

localfiend

Board Regular
Joined
Mar 15, 2006
Messages
166
Well, I have just finished up a really big huge project - and I want to have some fun with it just because. Can you use VBA to play wave files? The help has been no help - all it says is that the play function has been disabled. Its something I thought would be simple? Is it and I'm just missing something?

Thanks
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

yes you can
Code:
Private Declare Function PlayIt Lib "winmm.dll" Alias "sndPlaySoundA" _
    (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Sub PlaySound()
    If Application.CanPlaySounds Then
        'Substitute the path and filename of the sound you want to play
        Call PlayIt("c:\windows\media\chimes.wav", 0)
    End If
End Sub
kind regards,
Erik
 

localfiend

Board Regular
Joined
Mar 15, 2006
Messages
166
Good to hear that its possible - though I am a bit confused by your code. How would I stick it into a say a command button? I assume the code you posted is two separate subs?
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
it's a SINGLE sub, which needs some declaration on top

start the Visual Basic Editor (via Menu Tools, Macro or press ALT+F11).
On the Insert menu in the VBE, click Module. (if necessary)
In the module (the white area at the right), paste the your code

How to create a button and assign a macro to it:

If you don't already have the "Forms" toolbar active, click on Tools>Customize>Toolbars and check "Forms". The toolbar will appear on your screen; drag it down to the bottom of your screen to anchor it.

Click on the Button icon, and drag out a button on the screen. When you release the left mouse button, an "Assign Macro" dialogue box will appear. Highlight whatever macro you want, and click OK. The macro will now be assigned to that button.
 

localfiend

Board Regular
Joined
Mar 15, 2006
Messages
166

ADVERTISEMENT

Alrighty, I added the module - never done stuff like that before. I didn't really need to assign it to a button - I was just trying to see how I could stick it in some code. I think it makes sense now - I'll go and try it - all I should have to do then is create several different subs within that module with different sound links and then call each sub when I want it to play?
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
if you checked canplaysounds somewhere...
Code:
Private Declare Function PlayIt Lib "winmm.dll" Alias "sndPlaySoundA" _
    (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Sub test()
Call PlayIt("c:\windows\media\notify.wav", 0)
Call PlayIt("c:\windows\media\chord.wav", 0)
Call PlayIt("c:\windows\media\chimes.wav", 0)
Call PlayIt("c:\windows\media\tada.wav", 0)
End Sub
 

localfiend

Board Regular
Joined
Mar 15, 2006
Messages
166
Ok, the macro works fine, and i can get multiple sound file options. Now how would I go about calling in the middle of other code that I have? Can it not be done without disrupting what else I have going on?
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
disrupting ?
the code will wait till the sound has played before going further

think about using
Code:
Beep
code will continue during "beep" or at least not wait as long as the beep sounds (or how do you explain this in english :) )

proof: you will here the beep only once
(second beep is msgbox popup)
Code:
Sub test()
Dim i
Dim starttime As Double

starttime = Timer

    For i = 1 To 1000
    Beep
    Cells(1, 1) = Cells(1, 1) + 1
    Next i
    
MsgBox Timer - starttime

End Sub

compare to
Code:
Option Explicit
Private Declare Function PlayIt Lib "winmm.dll" Alias "sndPlaySoundA" _
    (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Sub test()
Dim i
Dim starttime As Double

starttime = Timer

    For i = 1 To 5
    Call PlayIt("c:\windows\media\ding.wav", 0)
    Cells(1, 1) = Cells(1, 1) + 1
    Next i
    
MsgBox Timer - starttime

End Sub
 

Forum statistics

Threads
1,141,415
Messages
5,706,306
Members
421,441
Latest member
VapesRub

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top