Hello:
Can somebody explain to me how to add a sound file to a macro. I need exact
instructions, since what I got (below) does not seem to work for some reason,
and error comes up. Can you please explain how to do it exactly?
Here is what I have:
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Sub PlayWavFile(WavFileName As String, Wait As Boolean)
If Dir(WavFileName) = "" Then Exit Sub ' no file to play
If Wait Then ' play sound before running any more code
sndPlaySound WavFileName, 0
Else ' play sound while code is running
sndPlaySound WavFileName, 1
End If
End Sub
Sub TestPlayWavFile()
PlayWavFile "c:\foldername\soundfilename.wav", False
MsgBox "This is visible while the sound is playing..."
PlayWavFile "c:\foldername\soundfilename.wav", True
MsgBox "This is visible after the sound is finished playing..."
End Sub
Office_Novice - 14 Aug 2008 19:32 GMT
Hi Art, I Have modified this a bit give it a try
Option Explicit
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Sub PlayWavFile(WavFileName As String, Wait As Boolean)
If Dir(WavFileName) = "" Then Exit Sub ' no file to play
If Wait Then ' play sound before running any more code
sndPlaySound WavFileName, 0
Else ' play sound while code is running
sndPlaySound WavFileName, 1
End If
End Sub
Sub TestPlayWavFile()
PlayWavFile "C:\WINDOWS\Media\Windows XP Startup.Wav", False
Debug.Print "This is visible while the sound is playing..."
PlayWavFile "C:\WINDOWS\Media\Windows XP Startup.Wav", True
Debug.Print "This is visible after the sound is finished playing..."
End Sub
> Hello:
>
[quoted text clipped - 24 lines]
> End Sub
>
Office_Novice - 14 Aug 2008 20:09 GMT
Siplified and commented good luck
Option Explicit
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
'EveryThing From Here Up Should be the First two lines of code
Sub PlayWavFile(WavFileName As String)
'This is its own procedure
sndPlaySound WavFileName, 1
End Sub
Sub TestPlayWavFile()
'Put this line of code in your macro
PlayWavFile "C:\WINDOWS\Media\Windows XP Startup.Wav"
End Sub
> Hello:
>
[quoted text clipped - 24 lines]
> End Sub
>
art - 14 Aug 2008 21:10 GMT
Looks like I am missing something. How do I start? I add a button from the
form control and add a new macro and copy and paste your vba? Please let me
know because this thing is "bugging" me. Thanks.
> Siplified and commented good luck
>
[quoted text clipped - 46 lines]
> > End Sub
> >
Office_Novice - 14 Aug 2008 21:45 GMT
Follow these steps
1) open Excel
2) Tools-->Macro-->Visual Basic Editor
This Is the "VBE"
3)in the VBE go to veiw-->project explorer
On the left hand side of the VBE you should see a file tree looking thing
5) Click VBA Project(yourfilenamehere)
that should reveal Sheet1,2, and 3 and this workbook
6) double click this workbook and Copy and paste this
Option Explicit
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Sub PlayWavFile(WavFileName As String)
sndPlaySound WavFileName, 1
End Sub
7) Double click the Sheet1 module and paste this
Sub TestPlayWavFile()
PlayWavFile "C:\WINDOWS\Media\Windows XP Startup.Wav"
End Sub
8) Close the VBE and return to excel
9) Veiw --> Toolbars --> forms
10) Drag the command button to the worksheet
11)Right Click the button -->Assign Macro
12) Select TestPlayWavFile
13) Click OK
14) Close the forms ToolBar
Test out your wav button good luck.
> Looks like I am missing something. How do I start? I add a button from the
> form control and add a new macro and copy and paste your vba? Please let me
[quoted text clipped - 50 lines]
> > > End Sub
> > >
art - 14 Aug 2008 22:04 GMT
I followed your instructions exactly and an error message comes up, "compile
Error: Sub or function not defined". What do I do now? please help. Thanks
for your help.
> Follow these steps
> 1) open Excel
[quoted text clipped - 88 lines]
> > > > End Sub
> > > >
art - 14 Aug 2008 22:05 GMT
I have Office 2007, if that makes a difference.
> Follow these steps
> 1) open Excel
[quoted text clipped - 88 lines]
> > > > End Sub
> > > >
Office_Novice - 15 Aug 2008 13:17 GMT
I will try the code on 2007 over the weekend and get back with you on monday,
Unfortunately i don’t have ‘07 at the office.
> I have Office 2007, if that makes a difference.
>
[quoted text clipped - 90 lines]
> > > > > End Sub
> > > > >
art - 15 Aug 2008 14:16 GMT
Thank you very much. I really appriciate it.
> I will try the code on 2007 over the weekend and get back with you on monday,
> Unfortunately i don’t have ‘07 at the office.
[quoted text clipped - 93 lines]
> > > > > > End Sub
> > > > > >
Office_Novice - 18 Aug 2008 16:33 GMT
OK, I tried this on xl 2007 and 2003 And had no trouble, Keep trying you'll
figure it out. Let me know if i can be more help.
Option Explicit
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Sub PlayWavFile(WavFileName As String)
sndPlaySound WavFileName, 1
End Sub
Sub TestPlayWavFile()
PlayWavFile "C:\WINDOWS\Media\Windows XP Startup.Wav"
End Sub
> Thank you very much. I really appriciate it.
>
[quoted text clipped - 95 lines]
> > > > > > > End Sub
> > > > > > >
art - 18 Aug 2008 16:48 GMT
It still says function not defined and PlayWavFile seems to be the problem.
Could it be that microsoft took out this function. I keep trying but does not
work. Am I missing something?
> OK, I tried this on xl 2007 and 2003 And had no trouble, Keep trying you'll
> figure it out. Let me know if i can be more help.
[quoted text clipped - 111 lines]
> > > > > > > > End Sub
> > > > > > > >
Mark de Niet - 08 Oct 2008 09:11 GMT
Hi, I see that I'm like what, almost 2 months late and you probably already found a solution, but I stumbled upon these posts since I was looking for a hint for how to play .wav files in excel (triggered by a time event, btw).
Had this error message all the time: "sub or function not defined", and the problem seemed to be with the PlayWavFile. The error didn't occur anymore after I put the "ThisWorkbook" class or reference or whatsitcalled in front of it. The Sub procedure is in my Module1 and apparently, if i call a procedure From the Module, I need to refer to it, not as "PlayWavFile", but "ThisWorkbook.PlayWavFile". Otherwise the compiler doesn't find it ("not defined")
Cheers