Home > Software engineering >  VBA - Powerpoint - Handling onClick Event
VBA - Powerpoint - Handling onClick Event

Time:06-07

I am developing a macro for PowerPoint in vba

My goal is to have one single Sub that 1-6 shapes were manually assigned to.

So the sub is executed when a shape is clicked and should determine with a Select case statement which of the shapes was clicked exactly (and therefore run a specific script)

I am struggeling to find Code snippets that show how the onclick event is handled and how I can determine for which shape the „onclick is True“

Can you guys help me with this? Thanks in advance!

CodePudding user response:

If you assign the following macro to each of the clickable shapes, it'll do what you're after in Slide Show view. Select the shape, choose Insert | Action and choose run Macro: HandleShapeClick.

You may need to modify it to run on Macs because of a bug in the VBA implementation there.

If you need something like this to function in Normal or Slide view, you'll need entirely different (and considerably more complex) code.

Sub HandleShapeClick(oSh As Shape)

    Select Case oSh.Name
        Case Is = "Rectangle 3"
            MsgBox "You clicked Rectangle 4"
        Case Is = "Rectangle 4"
            MsgBox "You clicked Rectangle 5"
        Case Is = "Rectangle 5"
            MsgBox "You clicked Rectangle 3"
        ' etc, for each add'l clickable shape
        
    End Select
    
End Sub

CodePudding user response:

You could assign a macro to your shape like that

Sub TestIt()

Dim sh As Shape
    
    Set sh = ActivePresentation.Slides(1).Shapes("<Name of your shape>")
        
    With sh.ActionSettings(ppMouseClick)
        .Action = ppActionRunMacro
        .Run = "showMsg"
    End With
    
End Sub

Sub showMsg()
    MsgBox "Ok"
End Sub

Or you go to Insert/Links group/Action in the ribbon

  • Related