In this post we see how to create a timer in PowerPoint that runs while on slide presentation mode. The principle is similar to what we saw with Excel timers (see post here: Excel Timer Macros). However, PowerPoint VBA does not support the Application.OnTime method, and therefore we can only run a timer in PowerPoint using either the Timer VBA function or the SetTimer function of the Windows API/User32 library. We will add the timer to a text box at the upper-left corner of the slide. You can download a ready-to-use PowerPoint TimerPPT add-in at the bottom of this article.
Add timer box
We can run the timer on a text box or any other shape. We can also use an ActiveX control (text box, label, other), or a userform, which allows further customization. Let´s start having a simple text box, which can be easily added manually or using the VBA code below.
Set pp = Application.ActivePresentation
Set shp = pp.Slides(1).Shapes.AddTextBox(1, 10, 10, 120, 50)
With shp
.Name = "Timer"
.TextFrame.TextRange.Text = Format(0, "h:mm:ss")
End With
The code adds a rectangular text box at the upper-left corner of the first slide in the PowerPoint presentation. The name of the text box is "Timer" and the text inside is "0:00:00". We can put that into a loop to add the timer box to every slide in the presentation.
Set shp = pp.Slides(n).Shapes.AddTextBox(1, 10, 10, 120, 50)
Next n
We probably want to start the timer while on slide show only. In such case, we need to add the text box or text boxes to each slide when starting the slide show, and then remove them when exiting slide mode. The code below removes the text box from slide 1 (we can have a loop as above to remove it from all the slides).
pp.Slides(1).Shapes("Timer").Delete
Timer events
We have seen three different options to run a timer event with VBA programming in Excel in this other post (Excel Timer Macros). We have two of those three options in PowerPoint VBA. The one missing is Excel´s Application.OnTime method, which is not available in PowerPoint VBA. Hence, we are left with either using the VBA Timer function or the SetTimer function of the Windows API/User32 library. We have covered both functions in several other articles throughout Excel Macro Mania´s blogs.
See below the code to declare and run the SetTimer and KillTimer functions (only available for Windows OS). SetTimer is set to call another macro (MoveTimer, via TimerEvent) every 1000 ms, which is used to move the PowerPoint timer every one second.
#If Win64 Then
Public Declare PtrSafe Function SetTimer Lib "User32" ( _
ByVal hwnd As LongLong, _
ByVal nIDEvent As LongLong, _
ByVal uElapse As LongLong, _
ByVal lpTimerFunc As LongLong) As LongLong
Public Declare PtrSafe Function KillTimer Lib "User32" ( _
ByVal hwnd As LongLong, _
ByVal nIDEvent As LongLong) As LongLong
Public TimerID As LongLong
#Else
Public Declare PtrSafe Function SetTimer Lib "User32" ( _
ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Public Declare PtrSafe Function KillTimer Lib "User32" ( _
ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
Public TimerID As Long
#End If
Sub StartTimer()
If TimerID <> 0 Then
KillTimer 0, TimerID
TimerID = 0
End If
TimerID = SetTimer(0, 0, 1000, AddressOf TimerEvent)
End Sub
Private Sub TimerEvent()
'On Error Resume Next
Call MoveTimer
End Sub
Sub StopTimer()
KillTimer 0, TimerID
TimerID = 0
End Sub
Hence, we just need to call the macro "StartTimer" when the slide show begins (or whenever we want the timer to run) and the macro "StopTimer" to stop it. It is also important to set TimerOn to true before calling StartTimer, as this will be set as condition to move the timer (see MoveTimer later). This is not entirely necessary when using the SetTimer function (we could avoid using the TimerOn variable altogether), but it is imperative when using the Timer VBA function (see just below).
The User32 library is only available for Windows OS. There is an alternative for Mac users: The native Timer VBA function. The macro below runs a timer using the Timer VBA function. The disadvantage of using the Timer VBA function is that code execution is not interrupted as it stays within the same procedure while running. As a result, performance is worse and errors may occur.
startT = Timer
Do While TimerOn = True
DoEvents
runT = Timer
If runT - startT >= 1 Then
Call MoveTimer
startT = runT
End If
Loop
Move timer
Both options explained above call another macro that updates the time value in the text box every one second. One way to have the timer running all through the presentation slide show is having a text box added to each slide and updating the time value for each text box. Another way (more complicated) is adding a text box to the active slide only (removing the box from the previous slide), keeping the time value in a variable. The macro below corresponds to the first option and adds one second to whatever time value in the text box for each slide in the presentation.
Dim shp As Shape
If TimerOn = True Then
For n = 1 To pp.Slides.Count
For Each shp In pp.Slides(n).Shapes
If shp.Name = "Timer" Then
With shp.TextFrame.TextRange
.Text = Format(TimeValue(.Text) + TimeValue("00:00:01"), "h:mm:ss")
End With
End If
Next shp
Next n
End If
End Sub
A boolean variable (TimerOn) is set to true when the timer starts (probably when the slide show starts) and set to false when the timer stops. Note that the name of the text box added at the upper-left corner of each slide is always "Timer".
In case of using the Timer VBA function method, the variable TimerOn must be set to false to exit the loop and the timer macro altogether. Remember that TimerOn is declared at module level to keep that value in memory while running the timer in one macro while reacting to exiting the slide show or any other trigger to set it to false and stop the timer in another macro.
As mentioned earlier, we probably want to run the timer only while on slide show mode. Hence, we can have a macro starting presentation mode and calling the start timer routine (using either of the two methods shown before).
'Call AddBox 'if not added before
pp.Slides(1).Shapes("Timer").TextFrame.TextRange.Text = Format(0, "h:mm:ss") 'or loop to initialize timer for all slides
pp.SlideShowSettings.Run
Call StartTimer
End Sub
We probably want to stop the timer when the presentation show ends. There is an event that still works in older versions of PowerPoint that triggers when exiting the slide show. We can use that to stop the timer when the slide show ends (and eventually remove the text boxes). However, that event is not reliable and does not work in newer versions of PowerPoint.
TimerOn = False
Call RemoveBox
End Sub
Application events
The event used earlier (OnSlideShowTerminate) does not work in newer versions of PowerPoint. Other slide-related events have also been deprecated. As with Excel, PowerPoint Application object's events are not readily available and need to be enabled through a class module. Let´s add a class module (clsAppsEvents) and use WithEvents to enable all Application events in PowerPoint as shown below.
We can add event procedures within the class module to respond to events such as slide show begin or end. The two event procedures shown above will call another macro to start or stop the timer accordingly. But we need to initialize the class from a standard module first for it to work (see below InitializeApp).
Set AppClass.App = Application
TimerOn = False
End Sub
We should call that macro (InitializeApp) when PowerPoint opens to be able to use slide show events to start/stop the timer. However, PowerPoint presentation-open events only work for add-ins. The code below is used to run the InitializeApp macro every time a PowerPoint presentation opens when saved as a PowerPoint add-in.
Call InitializeApp
End Sub
This is different to what we have seen many times in Excel, where there is always a Workbook_Open event that triggers when the workbook opens and allows to run code without needing to be saved as an add-in. PowerPoint, on the contrary, only allows to run code on start when saved as an add-in. Find below two ready-to-use PowerPoint add-ins that show a timer on slide mode for Windows and Mac users.
Click Here to download TiemrPPT Add-In for Windows OS.
Click Here to download TiemrPPT_Mac (beta) Add-In for Mac OS (and Windows OS).
No comments:
Post a Comment