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
 

Some videos you may like

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Excel_newbie_85

New Member
Joined
Nov 30, 2011
Messages
20
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
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,779
Office Version
  1. 2010
Platform
  1. Windows
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
 

Excel_newbie_85

New Member
Joined
Nov 30, 2011
Messages
20
Hey,
That looks better but has an error:
Compile error
Ambiguous name detected:PlaySound
"Sub Play_Wav_File()" is highlight light blue
?!
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,779
Office Version
  1. 2010
Platform
  1. Windows
Delete the declaration you had before.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,454
Messages
5,596,228
Members
414,046
Latest member
mbeutler1203

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
Top