I know Excel can talk to when you open a file but is it possible
to play like a 10 or 20 second clip of music.
If this is possible where would I get the music from.
Thank you
ddh
Yes you can play custom sounds when a workbook opens. Add the following code to the 'ThisWorkbook' VBA Object within your spreadsheet :
Code:
Private Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Sub Workbook_Open()
sndPlaySound "C:\WINDOWS\Media\tada.wav", 0
End Sub
Change the file path and file name to whatever sound you want to play. If you want you can declare a string variable and pass that variable value through to the sndPlaySound function. The 0 at the end signifies that the code must wait for the sound to finish before continuing to run . If you want the code to keep running while the sound is playing, change the 0 to 1.
As for your question of 'where would I get music from?' - you can use some of the standard sounds that ship with Windows - just like the example I provided. If you want to create your own sounds etc then you may want to search on Google - there are plenty of tools available to do this sort of thing.
Thank you for your help.
I posted the code into the Sheet "This Workbook"
and when I open the file I get this error.
"Compile Error"
Only comments may appear after End Sub, End Function, or End Property.
I don't understand what to do.
Thank you for your help.
ddh
It sounds like you may have misunderstood my instructions.
Undo what you have done so far, open your spreadsheet, click on menu option 'Tools' > 'Macros' > 'Visual Basic Editor' > this will open the Visual Basic Editor (VBE) screen. On the left hand side of the VBE screen select the 'Microsoft Excel Object' titled 'ThisWorkbook' and then copy / paste the code above on to right hand side of the screen. Save and close the VBA editor. If it still doesn't work, can you post the code you have used in the VBE screen?
I inserted the code into a New Sheet and it worked perfect.
I inseted the code into a sheet with other macros in it and
I still get the same error.
I don't understand why it will work on a new sheet but not a current sheet.
Where do I look for other sounds in my current XL files.
Thank you for your help.
ddh
I figured out what I was doing wrong.
It not easy being me.
I still am trying to find where and how I can use other sounds other then the one that you gave me.
sndPlaySound "C:\WINDOWS\Media\tada.wav", 0
I am guessing that the "tada.wav" is the file and if I look on my computer for ".wav" that there will be a lot more out there for me to use.
Thank you for your help.
ddh
I went to the "Windows", "Media" and I found all of the "wav" files.
I clicked on the play and heard each one of them but I could not find where the "tada" was listed. I checked properties and could not find it listed there either.
I was trying to see what other sounds that I had to choose from.
ddh
With your help I found out that music could be played when a file is opened but can music also be in a macro. Example - like clicking a button to move from one sheet to another and it plays a wav file.
I have still not found the other ".wav" file names.
ddh
Hi
Did you get the code to work with a sound other than the 'tada' sound? I only used that one because it was on my PC. You could try something like "C:\WINDOWS\Media\ding.wav" if that exists on your PC. Do you mind my asking why do you want to play music when changing tabs? Won't that be REALLY annoying? By the way, I can't get this method to work with mp3 files, only wav files - I haven't tried any others yet.
Andrew
Andrew - Yes I was able to make it play music when the file is opened.
Thank you for your code on that.
I understand now the name tada can be changed to "chord" or "ding"
or any of the other names.
I wass trying to see if there was any way that wav could be played on a macro just to see if it was possible. You are correct about it being
annoying.
ddh
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.