We've had a few forum questions recently asking how to play audio files in Excel.
Normally I'd just use my media player for this but I can see that you might want to have an audio or video file that may explain something about your workbook. Or you could use Excel to play videos in order to do some training of staff.The ability to do this kind of thing is there, so in this post I'm going to show you how to do it.
ActiveX Controls
You're going to need to insert some ActiveX controls so you will need to enable the Developer tab if you haven't already done this.
Required Controls
I'm going to use the Windows Media Player control, and a Command Button we can click to move to the next audio/video file.
To insert these, from the Developer tab, click on Insert, then More Controls. From the pop-up window, scroll down to Windows Media Player, select it and click OK.
You'll now see that your mouse pointer has changed to a cross. Click on the sheet and drag the box to the size you want the player to be.
Using a command button will allow us to click on it and load the next audio/video file. Click Insert, click on the ActiveX Command Button, and drag it to the size and position you want.
Editing Control Properties
By default the name and text on the button are CommandButton1, I want to change this. First, make sure you are in Design Mode.
On the Developer tab, Design Mode mode should be colored/highlighted to indicate that you are in Design Mode. When this is the case the controls won't carry out their usual functions.
Right click on the button and click on Properties. In the Properties window, I've changed the caption to 'Next' and changed the button's name to 'NextMediaFile'. We use the name in our VBA code so it's important to have something meaningful.
Loading Media Files into the Player
We'll come back to the VBA that loads the media files in a second, first I want to look at how we know what to play.
What I have done is use a specific cell, A3, to tell me the folder where the files are, and below that is a list of the files I want to play.
Please note that the filename must include the extension.
You could create a playlist in Windows Media Player and load that, and it will play everything in the playlist.
VBA
We want to click the 'Next' command button to load the next file so to do that we need a little VBA
Make sure you are in Design Mode, and right click the button. Click on 'View Code' and the VBA editor will open with an empty sub already created for you.
Note that the name of the sub is taken from the name we gave the button. Now you just need to enter the code to do the fancy stuff.
Download the VBA & Workbook
Enter your email address below to download the sample workbook.
How the Code Works
Each time the button is clicked the ActiveCell is moved down one row.
The code uses the ActiveCell to load the next file. If that cell is empty it goes to the start again, cell A4.
The file listed in the ActiveCell is loaded into the media player.
The code makes the assumption that there isn't anything else on the sheet, so if you happen to have F34 selected, it won't have anything in it. That being the case, the code will go back to the start of the media files and play the first one.
George
Nice but it doesn’t play youtube videos.. So if you send the excel file to another person he will never see the video because it is saved in your local hardrive… I also tried with cloud and onedrive it doesn’t work.. Any idea how to play an online video ???? Media player (even if at 2022) cannot play youtube videos
Philip Treacy
Hi George,
There’s an Add-In called Web Video Player that seems to be able to play YT vids. You can load if from the Ribbon -> Insert -> Get Add-Ins
regards
Phil
Stephen Luke
I want to add a wav file and include a start, pause and a stop button for the audio files. I don’t want Microsoft media player I just want the audio file on the spreadsheet with the buttons. At time I may have 3 or 4 recording (wav file) that I need to put in my report but I cant work out how to add the buttons. If I put the Microsoft media player onto the spreadsheet it takes up a lot of real estate and I only need the audio. Can you help
Philip Treacy
Hi Stephen,
You need something to play the audio and that’s the Windows Media Player. You can reduce the size of the WMP control by shrinking it using the control’s pull handles so that only the buttons/play bar are visible.
If you want further assistance please open a topic in our forum and attach your file.
Regards
Phil
Simon Jacobs
Thanks for helping me add audio to a spreadsheet, but could you describe how to start an audio track when the spreadsheet opens? And also not display the media player. (Does it matter what the default player is or if the user has an audio player at all?)
It would only be one mp3 file that would be looped unless the user chooses to pause the audio from a supplied button.
Catalin Bombea
Hi Simon,
To play the file in loop, you have to add this code:
WindowsMediaPlayer1.URL = “c:\test.mp3”
WindowsMediaPlayer1.Controls.play
WindowsMediaPlayer1.settings.setMode “loop”, True
To play when the file is open, you have to use the Workbook_Open event that should call your procedure.
Workbook & Worksheet Events in Excel VBA
Sandeep Kothari
Great stuff. Are ActiveX controls safe to use? Is there a similar feature under forms control?
Philip Treacy
Thanks Sandeep.
ActiveX controls are as safe as macros. If you trust who wrote it, or you know what the control does then it shouldn’t pose a problem for you.
There is no multimedia player controls in the Forms Controls.
Regards
Phil
Sunny Kow
Hi Philip
Is the ActiveX Windows Media Player you embedded same as the version that comes together with Windows?
The WMP that comes with Windows 10 can show subtitles/captions if the associated SRT files are present. Just exploring to see if the captions can be shown as well in Excel.
This question was asked before about 2 years ago at the forum.
https://www.myonlinetraininghub.com/excel-forum/vba-macros/playing-a-window-media-players-playlist-in-excel#p2031
Philip Treacy
Hi Sunny,
Yes, I just selected the control from the list in Excel. I didn’t install anything different before that.
If I watch a video in VLC that has subtitles, the subtitles are shown. If I watch the same video in WMP, the subtitles are not shown. From a quick search it appears I’d have to install extra software to get the SRT subtitles to appear in WMP.
Phil
Lucas
That was really helpful, thank you. I was just wondering if you could explain to me what is going on at “ActiveCell.Offset(1, 0).Select”, when you have the name of the thing and a dot that means it’s an event right? and I know that 1 means on and 0 off, can you tell me what you did in that line?
Philip Treacy
Hi Lucas,
the . indicates that you using a method (something you can do) or a property of whatever preceded the .
So for ActiveCell.Offset(1, 0), Offset is a method that moves the focus from the ActiveCell to another cell specified by (1, 0), or whatever values you use there. In my case (1, 0) means move down 1 row and across 0 columns.
I then use the Select method to select that cell specified by Offset(1, 0)
Regards
Phil
Duncan Williamson
Thanks for sharing this. As a non ActiveX user until now, I had no idea this was possible.
I can see many uses for it in my work
Philip Treacy
Great. It’s always good when we realise new things are possible 🙂
Boni Woodland
Thank you So very much for this clear, amazing learning event! I did it -It worked! I am so very jazzed!!!
Philip Treacy
You’re welcome Boni
Victoria
Thank you for the post. Is there a way to use a URL for the video or audio files to send the Excel file to someone to run from their computer?
Philip Treacy
Hi Victoria,
Do you mean use a URL to specify the location of a media file, or use a URL to open the Excel file from a web location?
Regards
Phil