playing .wav file sounds from Excel

Mreq

New Member
Joined
Feb 3, 2011
Messages
8
I would like to be able to play.wav files repeatedly from within Excel as part of an application I am developing. Users of my VBA application would likely use Excel 2007 and Excel 2010, and possibly Excel 2003. They could be using anything from Windows XP to Vista to Windows 7.

To simplify the distribution of my application, I want to be able to use embedded .wav files rather than reference separate .wav files stored apart from the Excel file. Because of this, I don’t think the approach of using sndPlaySound32 / winmm.dll will work. Is there any way to use those with an embedded .wav file?

If I use the approach of inserting the .wav file as an object, I think the user’s default media player for the .wav file gets opened when I play the file using the “Selection.Verb Verb:= xlPrimary” approach. I think my users will find this distracting to switch between Excel and Windows Media Player, for example. Is there any way that I can tell Excel to not pop up the media player but to instead leave it minimized or in the background?

I have looked at inserting the ActiveX control for Windows Media Player directly into the worksheet, but I can’t understand how I could get it to play a sound file that is embedded directly in the worksheet instead of using a URL to reference a stand-alone file. I think this issue of not being able to access embedded .wav files also applies if I insert the ActiveX control into a form and not directly into the worksheet. Am I correct that the ActiveX Windows Media Player control cannot refer to an embedded file?

I thought maybe I could insert PowerPoint file objects that automatically play sounds as part of the presentation when I selected the embedded object and then call “Selection.Verb Verb:= xlPrimary”. This works, but a presentation window pops up on the screen (although for my purposes it seems less annoying than WMP), and I can only play the sound file once from Excel unless the user closes the presentation after each playing of the sound. I then tried copying the sound file object, calling “Selection.Verb Verb:= xlPrimary” to play the copy, and then using “AppActivate ThisWorkbook.Name” to switch back to Excel. This approach is deficient because every time I call the object to be played, another instance (is that the right term?) of the presentation gets left open (e.g., when I look in the Windows task bar, I see multiple entries for “PowerPoint Slide Show – [Presentation in Sheet1]”).

It would be nice if there was a way to close the instance of the PowerPoint presentation after every time its sound file plays, but I can’t figure out how to do that. I thought maybe I could embed a macro-enabled PowerPoint slide that would auto-close, but I think the relevant events won’t fire unless the user manually calls a PowerPoint macro to initialize the application object every time the PowerPoint instance is created. I don’t want to kill off the entire POWERPNT.EXE process, because the user may have other unsaved PowerPoint files open. When I call “Selection.Verb Verb:= xlPrimary”, is there a way I can get a pointer to the created instance so that I can later selectively close it? Or can I somehow communicate with the PowerPoint process after the sound has been played to close any windows or instances that are partially identified as “[Presentation in Sheet1]”?

I have seen approaches where Excel VBA code creates new slides and then does manipulations via a reference to it. Maybe I could copy the embedded sound file from Excel and paste it into a dynamically created PowerPoint slide, set a property to make it auto-play, and then close the presentation?

Thank you for your help!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
i always use this

in a standard module place this declaration

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


use this code to play your files as needed

Code:
PlayWavFile "D:\MyWaveFile.wav", False


you could change the file type to hide a wav somewhere. that might work?
 
Last edited:
Upvote 0
Hi, diddi. Thanks for your help. In the past I have used the approach you described to play external wav files, and it works pretty well when the Excel file and "supporting" external (non-embedded) .wav files are meant to be used just by me, but I was hoping to identify a way that did not require that I zip all of the files together and then have them be decompressed by users (they sometimes have trouble following directions).

Code:
PlayWavFile "D:\MyWaveFile.wav", False
you could change the file type to hide a wav somewhere. that might work?
I apologize if I am not creative enough, but could you please provide more guidance as to what you meant by hiding the wav file in the above quote? I'd like it to be embedded in the Excel file itself for the aforementioned packaging and distribution reasons, and I'm not sure if there is a clever way to have sndPlaySound reference an embedded object.

Maybe I could have an Excel macro detach the embedded .wav file objects and then use the sndPlaySound approach you described to play the detached files. Does anyone know the VBA code that would be used to select embedded .wav file objects and save / paste them to disk as external files?

Thanks again for your help.
 
Upvote 0
Hi, diddi. Thanks for your help. In the past I have used the approach you described to play external wav files, and it works pretty well when the Excel file and "supporting" external (non-embedded) .wav files are meant to be used just by me, but I was hoping to identify a way that did not require that I zip all of the files together and then have them be decompressed by users (they sometimes have trouble following directions).

I apologize if I am not creative enough, but could you please provide more guidance as to what you meant by hiding the wav file in the above quote? I'd like it to be embedded in the Excel file itself for the aforementioned packaging and distribution reasons, and I'm not sure if there is a clever way to have sndPlaySound reference an embedded object.

Maybe I could have an Excel macro detach the embedded .wav file objects and then use the sndPlaySound approach you described to play the detached files. Does anyone know the VBA code that would be used to select embedded .wav file objects and save / paste them to disk as external files?

Thanks again for your help.

Try the Shell command Shell (path, 0) to hide WMP
 
Upvote 0
I don't see where the answer was given to playing embedded files.
I can play a sound referencing it from the drive such as this:
Code:
Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Sub SoundWarning()
sndPlaySound32 "C:\Documents and Settings\user\My Documents\hotidle.wav", 0
End Sub

But I need to be able to do something like:
Code:
sndPlaySound32 "embedded object\hotidle.wav", 0

So, first, I need to figure out how to find the exact name of the embedded file and then the syntax to play it. Thanks
 
Upvote 0
Re: playing .wav file sounds from Excel **SOLVED**

This seems to work:

Code:
Sub PlayWav()
    Dim wsh As Worksheet
    Set wsh = ActiveSheet
    ' Playing the sound will activate Sheet1
    Worksheets("Sheet1").OLEObjects(1).Verb xlPrimary
    ' Return to original sheet
    wsh.Select
End Sub
where Sheet1 is the sheet in which the .wav file is embedded.
Place a command button from the Form Controls on each sheet, and assign PlayWav to it.


The object # will show in the name box when the object is clicked
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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