PDA

View Full Version : Solved: Play .wav sound file when userform opened



Gil
04-27-2010, 01:38 PM
Hello

I want to be able to play a .wav sound file when a userform is opened. The following code works ok when you enter your own file path etc. I have entered the code in a module and all works ok manually but I can't get it to work automatically when the userform opens.


Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_FILENAME As Long = &H20000



Private Sub Knock_Click()
PlaySound "C:\Knock.wav", 0&, SND_FILENAME
'/// where "C:\Knock.wav" gets replaced by your wav's path / name.
End Sub

All suggestions gratefully received.
Gil

omnibuster
04-27-2010, 01:46 PM
Try.
Private Sub UserForm_Activate()
Beep
PlaySound "C:\Knock.wav", 0&, SND_FILENAME
End Sub

Gil
04-27-2010, 10:01 PM
Hello omnibuster
Your suggestion worked a treat.This is what I added to suit my project.I inserted it in Microsoft Excel Objects/ThisWorkbook


Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_FILENAME As Long = &H20000


Private Sub Workbook_Open()


PlaySound "C:\Knock.wav", 0&, SND_FILENAME
'/// where "C:\Knock.wav" gets replaced by your wav's path / name.

Application.WindowState = xlMaximized
UserForm1.Show

End Sub

Maybe not the most challenging solution but for a novice like me a great deal of help. If anyone can tell me what the 0&, SND_FILENAME means it may help in my understanding.

Many thanks for all the help

Gil

GTO
04-28-2010, 01:10 AM
Hi Gil,

See if this helps: http://allapi.mentalis.org/apilist/PlaySound.shtml

Mark

Gil
04-28-2010, 05:58 AM
Hello GTO

Thank you for the link. Not sure I understand it yet but will be added to my reference library

Gil