Using Vba to run/open an audio file

Excel_newbie_85

New Member
Joined
Nov 30, 2011
Messages
20
A very cold hello as its -16 centigrade here and thought I should start working on my new project.

I have been searching through vba sites for a simple code input that enables me to have a macro play and audio file.

Now, I understand it may need to be saved as a wav file but cannot get anything to open.

Details to consider:
Macro is activated then, application is opened, file played, application closes

audio format is .wav (but could be mp3 or wmp)

I am using WMP to play file (but can change to another)

So, with that in mind, does anyone know the criteria for the code?
example code (obviously its not real code, just explanitary code)

Run macro playfile()
open.wmp.play."c: blah blah blah .wav"
close application

end

Cheers
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I have found this but has syntax error!/!/
Private Declare Function PlaySound Lib "winmm.dll"_<?xml:namespace prefix = o /><o:p></o:p>
Alias "PlaySoundA" (ByVal lpszName As String, _<o:p></o:p>
ByVal hModule As Long, ByVal dwFlags As Long) AsLong<o:p></o:p>
<o:p></o:p>
Sub Play_Wav_File()<o:p></o:p>
<o:p></o:p>
Dim sWAVFile As String<o:p></o:p>
<o:p></o:p>
On Error GoTo Err_Play<o:p></o:p>
<o:p></o:p>
sWAVFile = "C:\Temp\Windows XP Logon Sound.wav"<o:p></o:p>
<o:p></o:p>
PlaySound(sWAVFile, &O0, 0)<o:p></o:p>
<o:p></o:p>
Err_Play:<o:p></o:p>
If Err <> 0 Then<o:p></o:p>
Err.Clear()<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
End Sub
 
Upvote 0
Try this:

Code:
Private Declare Function PlaySound Lib "winmm.dll" _
        Alias "sndPlaySoundA" _
        (ByVal lpszSoundName As String, _
        ByVal uFlags As Long) As Long
 
Sub Play_Wav_File()
    Dim sWAVFile    As String
 
    sWAVFile = "C:\Temp\Windows XP Logon Sound.wav"
 
    On Error Resume Next
    PlaySound sWAVFile, 0
    If Err.Number Then Err.Clear
End Sub
 
Upvote 0
Hey,
That looks better but has an error:
Compile error
Ambiguous name detected:PlaySound
"Sub Play_Wav_File()" is highlight light blue
?!
 
Upvote 0
Delete the declaration you had before.
 
Upvote 0

Forum statistics

Threads
1,213,563
Messages
6,114,332
Members
448,566
Latest member
Nickdozaj

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