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

  • Random Phrases

    ' Use 'Option Explicit' at the top of each module. It will force you to declare
    ' all variables, but it will show you mistakes at compile time.
    ' In your particular case, the (compile) error would be
    '     'Compile error: Variable not defined',
    ' and 'ThisPresentation' would be highlighted.
    Option Explicit
    'Option Base 1 ' not recommended (default is 0)
       ' An array created by the 'Array()' function is zero-based unless
       ' 'Option Base' is set to '1' (see above) when it is one-based;...
    
    Sub RandomPhrase()
       
       ' ...To ensure a zero-based array no matter the value set by 'Option Base',
       ' use 'VBA.Array()' instead of 'Array()'.
       Dim Phrases() As Variant: Phrases = VBA.Array( _
           "Peace and love…", "Have a good day…", "Happy Holiday…", _
           "Blessings…", "One day at a time…", "Save for rainy days…")
       
       ' From the Docs:
       ' If 'Randomize' is not used, the 'Rnd' function (with no arguments) uses
       ' the same number as a seed the first time it is called,
       ' and thereafter uses the last generated number as a seed value.
       Randomize
    
       ' Generate a random index.
       ' From the Docs:
       '     'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)'
       ' Since the lower limit of a zero-based array is 0, you can simplify:
       '     'Int((upperbound - 0 + 1) * Rnd + 0)'
       '     'Int((upperbound + 1) * Rnd)'
       Dim RandomIndex As Long: RandomIndex = Int((UBound(Phrases) + 1) * Rnd) ' 0-5
       ' You might argue the following is the same:
       'RandomIndex = Int((UBound(Phrases) * Rnd) + 1)
       ' It isn't! It returns a random integer between 1 to 5 (0 not included).
       
       ' Store the random phrase in a variable.
       Dim SelectedPhrase As String: SelectedPhrase = Phrases(RandomIndex)
       'Debug.Print RandomIndex, SelectedPhrase
      
      ' Display the phrase in the textbox ("TextBox19").
       ActivePresentation.Slides(1).Shapes("TextBox19") _
           .TextFrame.TextRange.Text = SelectedPhrase
    
    End Sub