VBA to trigger a sound effect

H

Hotbird

Am looking for a simple way to make Excel play a midi or wav file, so that
spreadsheet can make reasuring sounds, or helpful comments at appropriate
stages. Has anyone tried this?
 
H

Hotbird

Many thanks Tom. Once again you come to my rescue. This is where your
suggestion led:

Option Explicit

'Windows API function declaration

Dim WAVFile As String
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000

Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
Function Alarm1(Cell, Condition)

On Error GoTo ErrHandler
If Evaluate(Cell.Value & Condition) Then
WAVFile = ThisWorkbook.Path & "\sound1.wav" 'Edit this statement
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
Alarm1 = True
Exit Function
End If
ErrHandler:
Alarm1 = False

End Function
Function Alarm2(Cell, Condition)

On Error GoTo ErrHandler
If Evaluate(Cell.Value & Condition) Then
WAVFile = ThisWorkbook.Path & "\sound2.wav" 'Edit this statement
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
Alarm2 = True
Exit Function
End If
ErrHandler:
Alarm2 = False

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top