R – How to capture a PowerPoint VSTO Text Changed Event


I'm developing a PowerPoint C# VSTO add-in. I want to be able to capture a text changed event whenever the Title text of a slide is changed.

How can I attach a custom event handler that will fire whenever the Title text is changed?

Best Solution

Two things: 1) this is in VBA, but should be easily portable to C# and VSTO, 2) The "text changed" thing is a bit tricky. I can get you as far as "are you in a Title box" - the rest is more trival. It has to do with finding original state versus any changes. Probably doable, I just haven't done it.

To hook a selection change in PPT VBA, you'll need one class and one module. In the class, put this:

Public WithEvents PPTEvent As Application
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
    With Sel
        If .Type = ppSelectionText Then
            Dim sh As Shape: Set sh = .ShapeRange(1)
            If sh.Type = msoPlaceholder Then
                originalText = sh.TextFrame.Text
                Dim placeHolderType As Integer
                placeHolderType = sh.PlaceholderFormat.Type
                If placeHolderType = ppPlaceholderTitle Then
                    MsgBox "this is a title placeholder"
                End If
            End If
        End If
    End With
End Sub

Name the class "clsPPTEvents". Then in any module, put the following:

Public newPPTEvents As New clsPPTEvents
Sub StartEvents()
    Set newPPTEvents.PPTEvent = Application
End Sub
Sub EndEvents()
    Set newPPTEvents.PPTEvent = Nothing
    Set newPPTEvents = Nothing
End Sub

Press F5 on the StartEvents and that will enable the hook. Press F5 on the EndEvents to disable it.

Related Question