Playing MIDI-files using VBA in Microsoft Excel

Soundfiles in MIDI-format are often long, so it might be necessary to stop playing
the sound (e.g. when the macro is finished). Here is an example:

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
        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


  1. A followup question - how does one alter the volume/velocity of a midi file? I'd like to use this in a simulation I'm playing with (for background music) with WAV files used for events. However, the MIDI files I'm using are too loud in comparison

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.