Playing a sound file from entry in cell

G

Guest

I have recently used and adapted code from a post on here which is shown
below and works great. However does anybody know how I can (or if it can be
done?) adapt this further so that entering a different "name" plays a
different sound file in the same cell i.e. C7 in this case.

Many thanks,
Nige


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

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
If Target.Value = "Lawrence" Then
sndPlaySound32 "C:\Documents and Settings\LAPTOP\My Documents\homer_31.wav", 0
End If
End If
End Sub
 
D

Dave Peterson

Maybe...

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

Private Sub Worksheet_Change(ByVal Target As Range)
dim mySoundFile as string
dim myFolder as string

myFolder = "C:\Documents and Settings\LAPTOP\My Documents\"

mysoundfile = ""
If Target.Address = "$C$7" Then
select case lcase(target.value)
case is = "lawrence" :mysoundfile = "homer_31.wav"
case is = "jimmy" : mysoundfile = "triple.wav"
end select
end if

if mysoundfile = "" then
'do nothing
else
sndPlaySound32 myfolder & mysoundfile, 0
end if

End Sub

=========
If you're using xl2002+, you may want to try:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
Application.Speech.Speak Target.Value
End If
End Sub
 

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