Microsoft Office Tutorials and References
In Depth Information
Windows API Calls
Private Declare Function PlaySound Lib “winmm.dll” _
Alias “PlaySoundA” (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Sub PlayWAV()
WAVFile = “sound.wav”
WAVFile = ThisWorkbook.Path & “\” & WAVFile
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
End Sub
In the preceding example, the WAV file is played asynchronously. This means that execution
continues while the sound is playing. To stop code execution while the sound is playing, use this
statement instead:
Call PlaySound(WAVFile, 0&, SND_SYNC Or SND_FILENAME)
Playing a MIDI file
If the sound file is a MIDI file, you’ll need to use a different API call. The PlayMIDI procedure
starts playing a MIDI file. Executing the StopMIDI procedure stops playing the MIDI file. This
example uses a file named xfiles.mid .
Private Declare Function mciExecute Lib “winmm.dll” _
(ByVal lpstrCommand As String) As Long
Sub PlayMIDI()
MIDIFile = “xfiles.mid”
MIDIFile = ThisWorkbook.Path & “\” & MIDIFile
mciExecute (“play “ & MIDIFile)
End Sub
Sub StopMIDI()
MIDIFile = “xfiles.mid”
MIDIFile = ThisWorkbook.Path & “\” & MIDIFile
mciExecute (“stop “ & MIDIFile)
End Sub
Playing sound from a worksheet function
The Alarm function, which follows, is designed to be used in a worksheet formula. It uses a
Windows API function to play a sound file when a cell meets a certain condition.
Search JabSto ::

Custom Search