.
This is one method ... certain there are plenty more :
Option Explicit
#If VBA7 Then
Declare PtrSafe Function PlaySoundAPI Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long
Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
#Else
Declare Function PlaySoundAPI Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
#End If
Const SND_LOOP = &H8
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Const WAV_FILE_PATH_NAME As String = "C:\Windows\Media\Chimes.wav" ' <== Change WAV file as required here.
Const SNOOZE_INTERVAL As Long = 10 'Secs ' <== Change snooze duration as required here.
Sub StartAlarm()
Application.OnKey " ", "MuteAlarm"
PlaySoundAPI WAV_FILE_PATH_NAME, ByVal 0&, SND_FILENAME Or SND_ASYNC Or SND_LOOP
End Sub
Sub StopAlarm()
Call MuteAlarm(Snooze:=False)
End Sub
Sub MuteAlarm(Optional ByVal Snooze As Boolean = True)
KillTimer Application.hwnd, 0
PlaySoundAPI vbNullString, 0, 0
Application.OnKey " "
If Snooze Then
Application.OnKey " ", "MuteAlarm"
SetTimer Application.hwnd, 0, SNOOZE_INTERVAL * 1000, AddressOf StartAlarm
End If
End Sub
In your code where you have this line : If Range("d3").Value = 0 Then Exit Sub
Change to :
If Range("d3").Value = 0 Then
'Call Play Sound macro here
Exit Sub
Bookmarks