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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,403
Messages
6,119,308
Members
448,886
Latest member
GBCTeacher

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
Back
Top