Sound Events in EXCEL

phxsportz

Well-known Member
Joined
Jun 11, 2006
Messages
1,985
Is there any way to trigger a sound within Excel/VB other then using the "BEEP" command ?

Can the Windows Sounds be accessed ?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Think this code came from Joe Was.
Code:
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
Change wav file path and name as needed.
Code:
Sub PLAYWAV()
'Put in Sheet module, like: Sheet1.
'Or, Sheet Event or ThisWorkbook module!
Dim wavefile, x

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

wavefile = "C:\JRsounds\doh.WAV"
Call PlaySound(wavefile, SND_ASYNC Or SND_FILENAME)
End Sub
 
Upvote 0
A few steps:
In a Standard module, like: Module1 Add:

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 in the Sheet Module you want it to run or trigger on:


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


Or as an event:


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

If Target.Address <> "$S$7" Then Exit Sub
If UCase(Target.Value) = "YES" Then

PLAYWAV

End If
End Sub



Now you can also play MP3's through the Windows Media Player, with this code:


Private Declare Function ShellExecute _
Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long


Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Lo
ng

Sub myMP3Play()
'Sheet Module Code, like: Sheet1!
Dim thisMP3 As String, MediaPlayer As String, theFile As String

theFile = "Q3.mp3"
thisMP3 = "C:\Program Files\Windows Media Player\"


'Hide MediaPlayer and play!
'Call ShellExecute(FindWindow("xlMain", vbNullString), _
' "Open", theFile, vbNullString, thisMP3, 0)


'Minimize MediaPlayer and play!
'Call ShellExecute(FindWindow("xlMain", vbNullString), _
' "Open", theFile, vbNullString, thisMP3, 2)



'Show MediaPlayer and play!
Call ShellExecute(FindWindow("xlMain", vbNullString), _
"Open", theFile, vbNullString, thisMP3, 1)

End Sub

Sub myMP3()
'Sheet Module Code, like: Sheet1!
'Note: Once the media player opens you must hit the play button!

Dim myUnit, myTune

myTune = "C:\Program Files\Windows Media Player\Q3.mp3"
myUnit = Shell("C:\Program Files\Windows Media Player\WMPLAYER.exe /Play" & myTune, 1)

End Sub



This is how to hook the midi player:


Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

'// Function Discription:
'// The mciSendString function sends a command string to an MCI device.
'// The device that the command is sent to is specified in the command string.
'//
'// Success:
'// Returns zero if successful or an error.
'// The low-order word of the returned doubleword value contains the error return value.
'// If the error is device-specific, the high-order word of the return value is the
'// driver identifier; otherwise, the high-order word is zero.
'//
'// Parametre discriptions:
'// -lpszCommand
'// Address of a null-terminated string that specifies an MCI command string.
'// For more information about the command strings, see Command Strings.
'//
'// -lpszReturnString
'// Address of a buffer that receives return information.
'// If no return information is needed, this parameter can be NULL.
'//
'// -cchReturn
'// Size, in characters, of the return buffer specified by the lpszReturnString parameter.
'//
'// -hwndCallback
'// Handle of a callback window if the "notify" flag was specified in the command string.



'Const sMidiFile As String = "C:\WINNT\MEDIA\Passport.mid"
'Const sMidiFile As String = "H:\Excel\Passport.mid"

Const sMidiFile As String = "H:\Excel\Canyon.mid"
'Const sMidiFile As String = "H:\Excel\Midiex.mid"
'Const sMidiFile As String = "H:\Excel\Midibase.mid"


Dim Play

Sub Play_Midi()
'// Discription:
'// Plays a Midi sound File
'// Specified by Const sMidiFile
'//

Play = mciSendString("play " & sMidiFile, 0&, 0, 0)
If Play <> 0 Then MsgBox "Can't PLAY!"

End Sub

Sub Stop_Midi()
Play = mciSendString("close " & sMidiFile, 0&, 0, 0)
End Sub
 
Upvote 0
Once again..

You are 'Da man..

Thanks

I had found similar code to this but was unsure as to where to put it. This well documented slice of code helped immensly.

Thanks
 
Upvote 0
After MS removed the Play Sound from VBA, 1998?

I found the basic hook on MS's web site. About eight years ago Juan Pablo Gonzales and I played around with it, getting the memory addressing to work right and matching it to VBA. It works well.

I like to hook the media player for more control, but this basic hook works well.
 
Upvote 0
Just as a side note-update:

MS has changed the Media Player. It now has many additional features that make it better than ever. You can Rip & burn directly from it now and option it to the sky.

Unfortunately MS has come out with an updated version of its great bundled Photo Editor, so it stopped bundling it and now sells it as a stand-alone application in Windows XP and above. But, never fear MS will allow owners of past versions of Windows to re-install the old Photo Editor into new versions of Windows and even make it the default viewer rather than that striped-down Photo-Viewer that the now have subsitituted for the Photo-Editor. Complete instructions are available from MS on their web based help.


Here is the link:

http://office.microsoft.com/en-us/help/HP011454871033.aspx
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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