vbatextboxpowerpointonchange

Powerpoint VBA basics: textbox on change event


I have a slide presentation in Office 2010 and would like to use a textbox as input for a calculation and display the result in another textbox when the value changes.

With some help from StackOverflow I found the text box reference: Slides(2).Shapes(12) - so this code should do something? -

Sub Slide2_Shape12_Change()
    MsgBox "Hello!"
End Sub

..but nothing happens when I enter a new value in the box. Stuck already! Please excuse the simple question.


Solution

  • The code needs to be in the code-behind Module for the right Slide and the name of the Sub needs to be exactly right. The simplest way of doing this, once you have added the TextBox into the Slide, with the Developer tab showing in the PowerPoint ribbon, select the TextBox then click on the 'View Code' button. It should take you to the right Module and populate it with the 'signature' of the Sub which should be (if, for example, your TextBox is called TextBox1)

    Private Sub TextBox1_Change()
    
    End Sub
    

    Within this Sub, you can then do what you want eg send the text to the Immediate window

    Private Sub TextBox1_Change()
        Dim text As String
        text = TextBox1.text
        Debug.Print "'" & text & "'"
    End Sub
    

    ... or, if your other TextBox is called TextBox 2, you can display the text in it eg

    Private Sub TextBox1_Change()
        Dim text As String
        text = TextBox1.text
        TextBox2.text = text
    End Sub