My challenge is to be able to play MIDI files by clicking on an Excel version 2003 cell, in a list that contains many rows of MIDI file names (for each name there is a corresponding MIDI file). I can do this with the VBA code below which I found on the web and modified. A command button allows the playing to be stopped, otherwise after a minute or two, a file will reach the end and stop playing.
How can I prevent an attempt to play a second MIDI file while one file is playing? I am looking for a way to know that the file is either still playing or finished. When it is still playing, my stopping code works. If I try to run the stopping code when no file is playing, I get the usual error message.
I checked task manager processes while playing a MIDI file to see if a process gets added to the list, and it does not, so I can't test for a running process. Any ideas out there?
Jerry
Code:
Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
Public isMIDIPlaying As Boolean
Sub Play_MIDI(strMidiFile As String)
If isMIDIPlaying Then Exit Sub
If Application.CanPlaySounds Then
strMidiFile = ThisWorkbook.Path & Application.PathSeparator & strMidiFile
If (VBA.Dir(strMidiFile) <> "") Then
'The double quotes Chr$(34) are needed when the file path contains spaces.
'First OPEN and then PLAY.
mciExecute ("OPEN " & VBA.Chr$(34) & strMidiFile & VBA.Chr$(34) & " ALIAS jingle")
mciExecute ("PLAY jingle")
isMIDIPlaying = True
End If
End If
End Sub
Sub Stop_MIDI(strMidiFile As String)
If Application.CanPlaySounds Then
strMidiFile = ThisWorkbook.Path & Application.PathSeparator & strMidiFile
If (VBA.Dir(strMidiFile) <> "") And isMIDIPlaying Then
mciExecute ("CLOSE jingle")
End If
End If
isMIDIPlaying = False
Exit Sub
End Sub