Can you play sound via VBA?

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Using VB MIcrosofr has a MCI control (Media control interface)

this control connects to most spound sources.... guess you could use its properties to play a sound??

Ed
 
Upvote 0
Hi Tom
You can Play virtually anything you like
and in a number of ways.
What excatly do you want to do ??

If you like I can send you example files
to Play a Wave,Midi,MP3,AVI files located
either on your drive or embeded in an excel
File.....
 
Upvote 0
Thanks for the replies...
I created a spreadsheet for my daughter to practice her multiplication.
It is set up like a game with command buttons and a timer.
I'd like to add sounds for various events.

Wrong Answer
Right Answer
High Score
Timer Clicks
Ect...

I could find nothing in help...

Thanks again!
Tom

P.S.
Thanks Ivan for the offer...
I'd love to have the example files...
If you have any kids, I'll send you the spreadsheet when I'm done...

Tom
This message was edited by TsTom on 2002-05-01 01:41
 
Upvote 0
Hi Tom
Just to get you started.
Why not record what you want eg...WRONG ANSWER, RIGHT ON etc....probably sound
better coming from Mum :)
Then just use this code to play the wave files you have recorded.

<pre/>
Option Explicit

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

Public Const SND_SYNC = &H0
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10

Dim SndFile As String
Dim wFlags As Double
Dim PlayIt

Sub WAVPlay(File)
Dim SoundName As String

SoundName = File

wFlags = SND_ASYNC Or SND_NODEFAULT

PlayIt = sndPlaySound(SoundName, wFlags)

End Sub

Sub Wrong()
SndFile = "C:MEDIAwrong.wav"

If Dir(SndFile) = "" Then MsgBox "Sorry no File to Play!": End

PlayIt = sndPlaySound(SndFile, SND_ASYNC)

End Sub


</pre>

Tom....I have Kids would appreciate a copy
 
Upvote 0
What you have right there is great, Ivan...
Would like to know how to play midi files as well.
Thanks!
 
Upvote 0
On 2002-05-01 02:32, TsTom wrote:
What you have right there is great, Ivan...
Would like to know how to play midi files as well.
Thanks!

<pre/>
Option Explicit


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:WINDOWSMEDIAimposs[1].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



</pre>
 
Upvote 0
Hi Tom --

I would like a copy too if that is okay when youre done. My kid will do these things as long as they are "cool."

thanks!!
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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