Search code examples
powerpointactivex

How to replace text in Powerpoint with activeX textbox value?


I'm sorry if I can't explain this very well. Language barrier and all that. Might explain why I wasn't able to google a solution... Anyway. I'm trying to make an interactive Powerpoint where in the first slide the user writes their name in an ActiveX textbox and the name then repeats in several other slides. Because the name would be in the middle of different texts my solution was to add a placeholder word that would then be replaced with the textbox value. I was able to Frankenstein a code that replaces one name with another that works well enough but I can't get the textbox to work with it. Best I can do is replace the placeholder name with some variation of TextBox1.Value and while I know what causes that issue I can't work out a solution. Not sure if it makes a difference but the plan is to add some sort of "OK" button next to the active textbox that runs the macro.

Sub Findandreplace()
Dim sld As Slide
Set sld = ActivePresentation.Slides(1)
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "word1", "word2")
End If
End If
Next shp
Next sld
End Sub

Solution

  • ActiveX objects have a different set of properties/methods than regular PPT shapes. Assuming you have a reference to the AX textbox in a variable oSh,

    osh.OLEFormat.Object.Text
    

    will give you the text in the ActiveX textbox