无法使用VBA Excel播放Midi文件

时间:2018-10-27 09:41:29

标签: excel vba excel-vba

enter image description here我正在尝试使用VBA编辑器播放Midi文件。但是在分配宏之后 它显示为“当MIDI文件开始播放时单击确定...”。.但是歌曲没有播放。歌曲和excel都在同一文件夹中。 那么,谁能告诉我错误在哪里?怎么了?

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

Sub PlayMidiFile(Indiana_Jones_And_The_Last_Crusade__Main_Theme As String, Play As Boolean)
    If Dir(Indiana_Jones_And_The_Last_Crusade__Main_Theme) = "" Then Exit Sub ' no file to play
    If Play Then
        mciExecute "play " & Indiana_Jones_And_The_Last_Crusade__Main_Theme ' start playing
    Else
        mciExecute "stop " & Indiana_Jones_And_The_Last_Crusade__Main_Theme ' stop playing
    End If
End Sub

Sub TestPlayMidiFile()
    PlayMidiFile "C:\Users\nulik\Desktop\music sairam", True
    MsgBox "Click OK when the MIDI file starts playing..."
    MsgBox "Click OK to stop playing the MIDI file..."
    PlayMidiFile "C:\Users\nulik\Desktop\music sairam", False
End Sub

1 个答案:

答案 0 :(得分:1)

您需要使用midifile名称和文件扩展名(“ .mid”或“ .midi”)定义midifile的完整路径:

PlayMidiFile "C:\Users\nulik\Desktop\music sairam\Indiana_Jones_And_The_Last_Crusade__Main_Theme.mid", True 

PlayMidiFile "C:\Users\nulik\Desktop\music sairam\Indiana_Jones_And_The_Last_Crusade__Main_Theme.mid", False 

编辑:我在Excel 2010-32位和2016-64位上都可以使用它:

如果有64位系统,请在“专用声明功能”中添加PtrSafe。 32位系统可能具有PtrSafe。执行“ Sub TestPlayMidiFile”以播放midi文件。

Private Declare PtrSafe 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 "G:\Till\robert_miles__fable__dream_remix__unknown.mid", True
    MsgBox "Click OK when the MIDI file starts playing..."
    MsgBox "Click OK to stop playing the MIDI file..."
    PlayMidiFile "G:\Till\robert_miles__fable__dream_remix__unknown.mid", False
End Sub