Dutch Gemini's Weblog Pages

April 3, 2008

Playing a MIDI file in VBA

Filed under: Excel,Me,Programming — dutchgemini @ 9:38 am

I was hunting for a VBA code snippet that allowed me to play a sound file (in my case a MIDI file) when opening an About… dialogue form in my Excel Add-In. Many sources gave me input but non did work, not only apparently but also in the facts.
After some investigation (and the lack of exhaustive documentation from Microsoft) I discovered that with just a couple lines of code, I was able to have my tune play while watching the abouts.

In the form’s code modules declaration section I added this line:

Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long

In some examples I found the above Lib statement referring to a wrong library, being libnm.dll. The correct library reference is libmm.dll with 2 m’s which stand for ‘multimedia’.

In the form’s events, I added the necessary mciExecute statements to play my MIDI file (the extra stuff is there to be sure the application can play sounds and that the file we want to play is physically available):

Private Sub UserForm_Initialize()
On Error Resume Next
    Dim strMidiFile As String
    If Application.CanPlaySounds _
    Then
        strMidiFile = ThisWorkbook.Path & Application.PathSeparator & "My Jingle.mid"
        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")
        End If
    End If
End Sub
Private Sub UserForm_Terminate()
On Error Resume Next
    Dim strMidiFile As String
    If Application.CanPlaySounds _
    Then
        strMidiFile = ThisWorkbook.Path & Application.PathSeparator & "My Jingle.mid"
        If (VBA.Dir(strMidiFile) <> "") _
        Then
            ' Just CLOSE, also stops playing.
            mciExecute ("CLOSE jingle")
        End If
    End If
End Sub

Initially I did not hear any sound, so I checked the sound settings and discovered that the SW Synth volume was down to zero. After setting it to a reasonable volume, I could finally enjoy the music. Sometimes Windows Media Player mutes the SW Synth volume. I have seen this happen when you let WMP play MIDI sounds.

The mciExecute command string accepts more parameters than the one I’ve put, but I could not find working examples nor a reference. I tried to control the volume using the command string SETAUDIO VOLUME TO 250 (range is from 1 to 1000) but it did not work (volume did not change); other statements generated errors.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: