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


Friday, March 3, 2017

Set and get slide Activex controls properties from module

For PowerPoint VBA, there are two ways to control (access) ActiveX controls in certain slide from standard module.

Assume we want to get or set the value of toggle button, then we can do this using one of the following worked examples:

Example 1:

In module code, use the following code:

' Declare a public variable for toggle button value in module code
Public ToggleBtnValue As Boolean

' Sub-routine to call another sub-routine in slide 1
Sub SetToggleBtnValue()
Call Slide1.UpdateToggleBtn
End Sub

In slide 1 code, use the following code:

' In slide 1 code
Sub UpdateToggleBtn()     'Don't use "Private Sub"
MyToggleBtn.Value= ToggleBtnValue
End Sub

Example 2:

In module code, use the following code:

' Read toggle button value in slide number 1

' Set value of toggle button in slide number 1

The previous methods can be used to read or write any of the ActiveX control properties like back color, font name, font size, ... etc.