MIDI形式のサウンドファイルは長いことが多いため、サウンドの再生を停止する必要がある場合があります(マクロが終了したときなど)。次に例を示します:

Private Declare Function mciExecute Lib "winmm.dll" _

(ByVal lpstrCommand As String) As Long

Sub PlayMidiFile(MidiFileName As String, Play As Boolean)

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

If Play Then

mciExecute "play " & MidiFileName ' start playing

Else

mciExecute "stop " & MidiFileName ' stop playing

End If

End Sub

Sub TestPlayMidiFile()

PlayMidiFile "c:\foldername\soundfilename.mid", True

MsgBox "Click OK when the MIDI file starts playing..."

MsgBox "Click OK to stop playing the MIDI file..."

PlayMidiFile "c:\foldername\soundfilename.mid", False

End Sub