Macro to play wave file in current directory

Y

yo beee

Hello all,
I am using the following code to play a wave file and it works
perfectly.

Sub PlayMe3()
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Dim retval As Long
retval = PlaySound("C:\ABCDirectory\ACDB\wavtest.wav", _
0, SND_ASYNC Or SND_FILENAME)
End Sub

However, if the directory name is changed it will not work without changing
the name in the macro as well. Can anyone suggest a change in the macro to
play the wave file in the directory what ever the name of the directory is?
The wave file is normally saved in the same directory as the excel file. So
basically the macro needs to call up the wave file in what ever the
directory is that the excel file resides in. I was thinking something like:

Sub PlayMe3()
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Dim retval As Long
retval = PlaySound("..\..\wavtest.wav", _
0, SND_ASYNC Or SND_FILENAME)
End Sub

....this did not work, by the way. Any help is appreciated.

TIA,
yo beee
 
M

Melanie Breden

However, if the directory name is changed it will not work without changing
the name in the macro as well. Can anyone suggest a change in the macro to
play the wave file in the directory what ever the name of the directory is?
The wave file is normally saved in the same directory as the excel file. So
basically the macro needs to call up the wave file in what ever the
directory is that the excel file resides in. I was thinking something like:

Sub PlayMe3()
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Dim retval As Long
retval = PlaySound("..\..\wavtest.wav", _
0, SND_ASYNC Or SND_FILENAME)
End Sub

try this:

retval = PlaySound(ThisWorkbook.Path & "\wavtest.wav", _
0, SND_ASYNC Or SND_FILENAME)

--
Mit freundlichen Grüssen

Melanie Breden
- Microsoft MVP für Excel -

http://excel.codebooks.de (Das Excel-VBA Codebook)
 
B

Bob Phillips

Put it in a function as a first step to add flexibility.

Then use ThisWorkbook for the call

PlayWavFile ThisWorkbook/Path & "\wavtest.wav"

if you want to run it in the activeworkbook.directory, use

Play WavFile ActiveWorkbook & "\wavtest.wav"

'-----------------------------------------------------------------
Public Function PlayWavFile(WavFile As String) As String
'-----------------------------------------------------------------
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
PlaySound WavFile, 0, SND_ASYNC Or SND_FILENAME
PlayWavFile = ""
End Function


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

playing wma files 2
Alarm 2 2
Playing wavfiles from excel 3
sound 4
Wavfile playing trubcated 3
Playing sound in VBA 3
HOW TO PLAY SOUND.WAV FILES SIMULTANEOUSLY UNDER EXCEL 3
Playing a sequence of WAV files 6

Top