#If VBA7 Then
Public Declare PtrSafe Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long _
) As Long
#Else
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long _
) As Long
#End If
' The sound is played asynchronously and the function returns immediately
' after beginning the sound. To terminate a sound called with SND_ASYNC, call
' sndPlaySound with lpszSoundName set to NULL
Public Const SND_ASYNC = &H1
' The sound plays repeatedly until sndPlaySound is called again with the
' lpszSoundName parameter set to NULL. You must also specify SND_ASYNC with this
'flag
Public Const SND_LOOP = &H8
' The parameter specified by lpszSoundName points to an image of a waveform
' sound in memory
Public Const SND_MEMORY = &H4
' If the sound cannot be found, the function returns silently without
' playing the default sound
Public Const SND_NODEFAULT = &H2
' If a sound is currently playing, the function immediately returns FALSE without
' playing the requested sound
Public Const SND_NOSTOP = &H10
' The sound is played synchronously and the function does not return
' until the sound ends
Public Const SND_SYNC = &H0
Sub PlayTheSound(ByVal WhatSound As String, Optional flags As Long = 1)
If Dir(WhatSound, vbNormal) = "" Then
' WhatSound is not a file. Get the file named by
' WhatSound from the Windows\Media directory.
WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
If InStr(1, WhatSound, ".") = 0 Then
' if WhatSound does not have a .wav extension,
' add one.
WhatSound = WhatSound & ".wav"
End If
If Dir(WhatSound, vbNormal) = vbNullString Then
' Can't find the file. Do a simple Beep.
Beep
Exit Sub
End If
Else
' WhatSound is a file. Use it.
End If
' Finally, play the sound.
sndPlaySound WhatSound, flags
End Sub