Sound clips in Excel

eGiauque

New Member
Joined
Apr 6, 2007
Messages
17
I'm trying to write some VB code that will call up a sound file each time I give a new cell focus.

Example:

Cell A1 holds value "Hello"

I'm trying to write code that calls a sound clip I've recorded that says "Hello" when I select A1

Any suggestions?

Many Thanks!!!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
See http://www.j-walk.com/ss/excel/tips/tip59.htm

This goes in a regular module:

Code:
Declare Function PlaySound Lib "winmm.dll" Alias _
         "sndPlaySoundA" (ByVal wavFile As String, _
        ByVal lNum As Long) As Long

and this goes in the worksheet's code module (right click the sheet tab, View Code).

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(False, False) = "A1" Then Call PlaySound("sinkdr.wav", 0) 'assumes WAV file is in the same folder
End Sub
 
Upvote 0
It takes a few steps:

in a Standard code module, like: Module1 put:


Declare Function PlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszName As String, ByVal dwFlags As Long) As Long

'Put in Standard module, only, like: Module1!
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000


Then on the sheet's code module that you want the event to play the sound put:


Sub PLAYWAV()
'Put in Sheet module, like: Sheet1.
Dim wavefile, x

'List the "Drive:/Folder/filename.wav" for: wavefile!
'Or put the wavefile in the same folder as your WorkBook!


wavefile = "Ricochet.WAV"

Call PlaySound(wavefile, SND_ASYNC Or SND_FILENAME)
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Put in Sheet module, like: Sheet1.

If Target.Address <> "$A$1" Then Exit Sub
PLAYWAV

End Sub



Note: Make sure you have the WAV file in the same folder as the Workbook or indicate the Drive Folder File Extension when indicating the wavefile to play!
 
Upvote 0
Next Step - multiple wav files

This is excellent - I tried both sets of code and both work great.

This gets me part way for what I want to do.

I'm planning to populate each cell with foreign language words and I want excel to reference a .wav file that will be called that name.

Example:

Parlez.wav
Vous.wav
Francais.wav

How can I get the code the read the text string in the cell and assign it to the SoundfileName.wav?


wavefile = "SoundfileName.WAV"
 
Upvote 0
Also - need to read wav file from anywhere

I also want to ensure it reads values anywhere on the sheet, not just in cell A1
 
Upvote 0
If you activate Excels word speak ability and have the say english word or phrase in column "A" and say the chinese in column "D" written as it would be in english.

When the user selects a row this will have Excel speak it out for you:


Public myRow As String
Public myRngA As String
Public myRngD As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'Sheet module code, like: Sheet1, only!
myRow = Selection.Row
myRngA = "A" & myRow
myRngD = "D" & myRow

'MsgBox myRow & vbCr & myRngA & vbCr & myRngD

'Speak.

Range(myRngA).Speak
Range(myRngD).Speak
End Sub
 
Upvote 0
To do it your way change the code to:

In a Standard Module like: Module1


Public myFile$

Declare Function PlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszName As String, ByVal dwFlags As Long) As Long

'Put in Standard module, only, like: Module1!
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000



Then on the sheet's code module that you want the event to play the sound put:


Sub PLAYWAV()
'Put in Sheet module, like: Sheet1.
Dim wavefile, x

'List the "Drive:/Folder/filename.wav" for: wavefile!
'Or put the wavefile in the same folder as your WorkBook!


wavefile = myFile & ".WAV"

Call PlaySound(wavefile, SND_ASYNC Or SND_FILENAME)
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Put in Sheet module, like: Sheet1.

On Error GoTo myEnd
myFile = Target.Value

PLAYWAV

myEnd:
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,073
Messages
6,182,700
Members
453,132
Latest member
nsnodgrass73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top