Is it possible to link a WAV file to a command button using VBA. My VBA skills are very limited and I have been unsuccessful in finding a way of playing a WAV file by clicking a button on a form.
I've successfully added capability to play wav and midi files from Visual Basic buttons. Below is the basic code needed to play the files. I've stripped out all the code that filled listboxes with audio files.
Public Sub PlaySound(strSoundfileName As String)
Dim bReturn As Boolean
If Right(strSoundfileName, 4) = ".wav" Then
Soundname$ = strSoundfileName
wFlags% = SND_ASYNC Or SND_NODEFAULT
x% = sndPlaySound(Soundname$, wFlags%)
Else
bReturn = PlayMidiFile(strSoundfileName)
End If
End Sub
'Basic Wave Playing by J W Lehman
Declare Function sndPlaySoundx Lib "MMSYSTEM.DLL" (ByVal lpszSoundName$, ByVal wFlags%) As Integer
Public Declare Function sndPlaySound Lib "winmm" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Global Const SND_SYNC = &H0
Global Const SND_ASYNC = &H1
Global Const SND_NODEFAULT = &H2
Global Const SND_LOOP = &H8
Global Const SND_NOSTOP = &H10
Type tagOPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strFile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
' Note: This code is from the Access 95 Developer's Handbook,
' by Paul Litwin, Ken Getz, Mike Gilbert, and Greg Reddick.
' (c) 1995 by Sybex.
' Used with permission
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Public Const glrOFN_READONLY = &H1
Public Const glrOFN_OVERWRITEPROMPT = &H2
Public Const glrOFN_HIDEREADONLY = &H4
Public Const glrOFN_NOCHANGEDIR = &H8
Public Const glrOFN_SHOWHELP = &H10
Public Const glrOFN_NOVALIDATE = &H100
Public Const glrOFN_ALLOWMULTISELECT = &H200
Public Const glrOFN_EXTENSIONDIFFERENT = &H400
Public Const glrOFN_PATHMUSTEXIST = &H800
Public Const glrOFN_FILEMUSTEXIST = &H1000
Public Const glrOFN_CREATEPROMPT = &H2000
Public Const glrOFN_SHAREAWARE = &H4000
Public Const glrOFN_NOREADONLYRETURN = &H8000
Public Const glrOFN_NOTESTFILECREATE = &H10000
Public Const glrOFN_NONETWORKBUTTON = &H20000
Public Const glrOFN_NOLONGNAMES = &H40000
Public Const glrOFN_EXPLORER = &H80000
Public Const glrOFN_NODEREFERENCELINKS = &H100000
Public Const glrOFN_LONGNAMES = &H200000
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
(ByVal lpString1 As String, ByVal _
lpString2 As String) As Long
Declare Function glr_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (ofn As tagOPENFILENAME) As Boolean
Declare Function glr_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (ofn As tagOPENFILENAME) As Boolean
'Play MIDI file
Private Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As String, ByVal uReturnLength As Long, _
ByVal hwndCallback As Long) As Long
Public Function PlayMidiFile(MidiFile As String) As Boolean
'MidiFile = File's Full Path
'Returns: True if successful, false otherwise
Dim lRet As Long
On Error Resume Next
If Dir(MidiFile) = "" Then Exit Function
'Stop any currently playing .mid
lRet = mciSendString("stop midi", "", 0, 0)
lRet = mciSendString("close midi", "", 0, 0)
'Play
lRet = mciSendString("open sequencer!" & MidiFile & " alias midi", "", 0, 0)
lRet = mciSendString("play midi", "", 0, 0)
PlayMidiFile = (lRet = 0)
Debug.Print "PlayMidiFile " & MidiFile
End Function
Public Function StopMidi() As Boolean
'Stops midi from playing
'Returns: True if successful, false otherwise
Dim lRet As Long
On Error Resume Next
'Stop any currently playing .midi
lRet = mciSendString("stop midi", "", 0, 0)
StopMidi = (lRet = 0)
lRet = mciSendString("close midi", "", 0, 0)
End Function
Function IsSupportedAudioFile(varFilename As Variant) As Boolean
Dim strExtension As String
strExtension = Right$(varFilename, 3)
Select Case strExtension
Case "MID", "WAV", "mid", "wav"
IsSupportedAudioFile = True
Case Else
IsSupportedAudioFile = False
End Select
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.