1

I'm trying to make a macro where it will change the text of the button that is clicked by the user.

I already tried the oShape declaration mentioned in Microsoft's forum. Here's what I have:

Sub changeLetter(oShape As Shape)
    If ActivePresentation.Slides(oShape.Parent.SlideID).Shapes(oShape.Name).TextFrame.TextRange.Text = "a" Then
        ActivePresentation.Slides(oShape.Parent.SlideID).Shapes(oShape.Name).TextFrame.TextRange.Text = "b"
    ElseIf ActivePresentation.Slides(oShape.Parent.SlideID).Shapes(oShape.Name).TextFrame.TextRange.Text = "b" Then
        ActivePresentation.Slides(oShape.Parent.SlideID).Shapes(oShape.Name).TextFrame.TextRange.Text = "a"
    End If
End Sub

What I have here is a toggle between "a" and "b".

EDIT: I decided to use command buttons instead:

Private Sub letter1_Click()
    If letter1.Caption = "a" Then
        letter1.Caption = "b"
    ElseIf letter1.Caption = "b" Then
        letter1.Caption = "a"
    End If
End Sub

EDIT 2: I tried using command buttons but I just keep ending up with the "Invalid outside procedure" error

EDIT 3: I got it to work! My apologies!

  • I used the same code as in your Edit above and popped it into a command button ... it worked exactly as expected. No errors. What line do you run into error on? – Steve Rindsberg Feb 21 '23 at 01:54
  • @SteveRindsberg It does not work when I click :( – Colurswitch Youtube Feb 21 '23 at 05:10
  • No errors but it just does not work – Colurswitch Youtube Feb 21 '23 at 05:11
  • Are you in slideshow view when you click this? It will only work there, not in normal view. Also, have you compiled the code you typed (Debug | Compile VBA Project in the VBA editor)? If there are errors during a slide show, PPT generally won't display any error messages. If it compiles, set a breakpoint on the first executable command (click the line in the editor, press F9) , then try it again. Use F8 to step through the code a line at a time to find out where the error, if any, occurs. – Steve Rindsberg Feb 22 '23 at 16:54

1 Answers1

1

Is there some reason you're starting with a known shape (handed to you in oShape) and backing up to the parent slide only to work your way down the object hierarchy to the shape again?

Assuming you're using Windows PPT, try this instead:

Sub changeLetter(oShape As Shape)
    With oShape.TextFrame.TextRange
        Select Case .Text
            Case Is = "a"
                .Text = "b"
            Case Is = "b"
                .Text = "a"
            Case Else
                ' do nothing
        End Select
    End With
End Sub

If you're on Mac, it Thinks Different, of course, but there's a way around it. Either way, this requires that you assign a Run Macro action setting so your macro runs when the shape is clicked, and it only works in slide show view.

Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34