PDA

View Full Version : 3 Inputs, 3 different Sounds



cpearce81
03-06-2012, 03:57 PM
Hi All

I am currently trying to create a VBA code to make 3 sounds depending on the input e.g.

cell = 1, sound = "Correct!"
cell = 0, sound = "Incorrect"
cell ="blank", no sound

I have looked for code but I can only see code for 1 sound or alarm. Can anyone assist?

I have a folder with my excel worksheet and the wav. files for correct and incorrect.

Thanks

Chris

GTO
03-07-2012, 12:51 PM
Greetings Chris,

Not sure what code you found, but you can use an API Function to play a sound and just use different files for the different values. Something along the lines of:


In a Standard Module:

Option Explicit

' Taken from an example at Chip Pearson's site:
' http://www.cpearson.com/excel/PlaySound.aspx

Public Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long _
) As Long

Public Const SND_SYNC = &H0 ' (Default) Play the sound synchronously. Code execution
' pauses until sound is complete.
Public Const SND_ASYNC = &H1 ' Play the sound asynchronously. Code execution
' does not wait for sound to complete.
Public Const SND_NODEFAULT = &H2 ' If the specified sound is not found, do not play
' the default sound (no sound is played).
'public Const SND_MEMORY = &H4 ' lpszSoundName is a memory file of the sound.
' Not used in VBA/VB6.
Public Const SND_LOOP = &H8 ' Continue playing sound in a loop until the next
' call to sndPlaySound.
Public Const SND_NOSTOP = &H10 ' Do not stop playing the current sound before playing
' the specified sound.

In the Worksheet's Module:


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Cells(1), Target) Is Nothing And Target.Count = 1 Then
Select Case Cells(1).Value
Case 0
If Not Cells(1).Value = vbNullString Then
sndPlaySound32 "C:\Windows\Media\Chimes.wav", SND_SYNC Or SND_NODEFAULT
End If
Case 1
sndPlaySound32 "C:\Windows\Media\Chord.wav", SND_SYNC Or SND_NODEFAULT
End Select
End If
End Sub

Hope that helps,

Mark