Use VBA to play a sound (.wav) file?


Posted by Pauk Magruder on April 03, 2001 8:11 PM

Can VBA be used to play a sound file (.wav) when A workbook opens?

Excel 97 with windows NT
Thanks in Advance..
Paul

Posted by mseyf on April 04, 2001 8:28 AM

Paul:

put this code in a module:

Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Public Sub PlayWavFile(WavFileName As String, Wait As Boolean)

If Dir(WavFileName) = "" Then Exit Sub 'no file to play

If Wait = True 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

and copy this into the ThisWorkbook code sheet:

Private Sub Workbook_Open()
PlayWavFile WavFileName:="C:\winnt\media\tada.wav", Wait:=True
End Sub


Have fun!

Mark



Posted by Paul Magruder on April 11, 2001 5:19 AM

Re: Thank You Very Much


Just what I Needed... Thanks