vbapowerpointactivex

Change ActiveX Textbox in PowerPoint


I have a form in Excel that will add text to existing named ActiveX textboxes in PowerPoint.

Dim PPTM_app As Object
Dim PPTM_file As Object

    Set PPTM_app = CreateObject("Powerpoint.Application")
        Set PPTM_file = PPTM_app.Presentations.Open(folder_path & ".PPTM_")
    
        With PPTM_file
            .Shapes("Title_Txt").OLEFormat.Object = "some text"
            .Shapes("Sub_Txt").OLEFormat.Object = "some text"
        End With
        
PPTM_file.Close SaveChanges:=True
Set PPTM_file = Nothing
Set PPTM_app = Nothing

The code is working to open the PPTM file but throws an error when .Shapes starts. "Object doesn't support this property or method (Error 438)". It seems to code to change ActiveX fields in Word is very different than PowerPoint.


Solution

  • There is not much difference between Powerpoint and Word - only that the collection of shapes belong to a Slide, not to the whole presentation.

    If you know exactly on which slide you have the textbox, you can simply write something like

    With PPTM_file.Slides(1)
       .Shapes("Title_Txt").OLEFormat.Object = "some text"
       .Shapes("Sub_Txt").OLEFormat.Object = "some text"
    End With
    

    If you are not sure on which slide you can find your textbox, you will need to loop over all slides. Note that every slide can have a shape (eg your textbox) with the same name - this happens for example when you copy a slide.

    I have created a small routine for that:

    Sub fillPPTextbox(presentation as Object, texboxName As String, text As String)
        Dim slide As Object    ' Use PowerPoint.Slide with early binding
        For Each slide In presentation.Slides
            Dim sh As Object   ' Use PowerPoint.Shape with early binding
            On Error Resume Next
            Set sh = Nothing
            Set sh = slide.Shapes(texboxName)
            On Error GoTo 0
            If Not sh Is Nothing Then sh.OLEFormat.Object = text
        Next slide
    End Sub
    

    I always recommend early binding. Add a reference to the Microsoft Powerpoint Object Library and change the code

    Sub fillPPTextbox(presentation as PowerPoint.Presentation, texboxName As String, text As String)
        Dim slide As PowerPoint.Slide 
        For Each slide In presentation.Slides
            Dim sh As PowerPoint.Shape 
        (...)
    

    In your code, call the routine like this:

        fillPPTextbox PPTM_file, "Title_Txt", "some text"
        fillPPTextbox PPTM_file, "Sub_Txt", "some text"