Sunday, March 5, 2017

PowerPoint VBA: play sound file programatically

On the run, In this post I will show a very simple way to run a sound file programmatically in PowerPoint VBA through the following steps:

[1] Insert an action button of type "Custom". Place it outside the slide area, so it will not be visible during the presentation.

[2] Right click the action button, a popup menu will appear. Select "Hyperlink..." and a new window named "Action settings" will appear.

[3] In "Action settings" window, check the option "Play sound" and select the .wav sound file you want to play from your computer. In most cases you will not have a .wav file so you have to convert to this format. You can do this online through this link Online converter

[4] Rename your shape from selection pane: select the action button, select "Format" tab in the ribbon, go to "Arrange" group and click on "Selection pane" button. In my case, I named the button "Dummy Button".

[5] Finally, It is the code time. You can play the sound with only a single line of code.


' Place this code in slide code
' Sub to play your pre-loaded sound file programmatically
Sub PlayMySound()
Shapes("Dummy Button").ActionSettings(1).SoundEffect.Play
End Sub


Or, instead of step 3, you can also load the sound file programmatically like the next code:


' Place this code in slide code
' Sub to load and play sound file programmatically
Sub LoadPlayMySound()
Shapes("Dummy Button").ActionSettings(1).SoundEffect.ImportFromFile ("C:\Users\Shady\Desktop\Finger-snap.wav")

Shapes("Dummy Button").ActionSettings(1).SoundEffect.Play
End Sub

Done...