vbapowerpoint

Powerpoint VBA - random phrases in a text box - Error 424 Object required


This is an issue with Powerpoint VBA. I want to have some random phrases to show up in a powerpoint presentation. I want the phrases to show in a text box inside a slide in the presentation I have a preset list of phrases. The code goes well 'most' of the time: I declare variables, get them properly loaded with values. The problem comes when I try to assign the random quote to the powerpoint object. The code error is "Runtime error 424" / Object required But I'm not able to debug the error. Could somebody help here? Thanks. This is the code:

Sub RandomPhrase()
   Dim phrases As Variant
   Dim phraseCount As Long   
   Dim randomIndex As Long   
   Dim selectedPhrase As String

   phrases = Array("Peace and love…", "Have a good day…", "Happy Holiday…", "Blessings…", "One day at a time…", "Save for rainy days…")
   phraseCount = UBound(phrases) ' or UBound(phrases) - LBound(phrases) + 1 
   ' Generate a random index
   randomIndex = Int((phraseCount * Rnd) + 1)
   ' Get the random phrase   
   selectedPhrase = phrases(randomIndex)
  ' Display the phrase in the textbox (assuming the textbox is named "TextBox19")
   With ThisPresentation.Slides(1).Shapes("TextBox19").TextFrame.TextRange  <<error 424 in this line>>
        .Text = selectedPhrase
   End With
End Sub

Solution

  • There is no ThisPresentation in PPT VBA

    See for example - How to simulate ThisPresentation in PowerPoint VBA