Search code examples
vbapowerpointms-office

How do I set an animation effect to trigger on click of a specific shape?


I am writing VBA to add an animation to an object.

My goal is to make the added animation trigger on click of another specific shape

Image of Trigger on Click object, Animated object, then Animation pane. The first animation is added by the script and happens while the slide starts. The second animation is manually added and is triggered on click of object. Image of Trigger on Click object, Animated object, then Animation pane. The first animation is added by the script and happens while the slide starts. The second animation is manually added and is triggered on click of object. (Look at image description please)

Sub AddAnim()
    ' Variables
    Dim shp As Shape
    Dim effAdd As Effect
    ' Get selected shape
    Set shp = ActiveWindow.Selection.ShapeRange(1)
    ' Add pulse effect to the shape
    Set effAdd = ActivePresentation.Slides(1).TimeLine _
    .MainSequence.AddEffect(Shape:=shp, _
    effectId:=msoAnimEffectFlicker, _
    Trigger:=msoAnimTriggerWithPrevious)
    effAdd.Timing.TriggerDelayTime = 0.5
End Sub

This is my first version of the code. It is working exactly as intended, which adds a colour pulse animation with the previous with a 0.5-second delay.

Sub AddAnim()
    ' Variables
    Dim shp As Shape
    Dim effAdd As Effect
    Dim trgBut As Shape
    ' Get selected shape
    Set shp = ActiveWindow.Selection.ShapeRange(1)
    Set trgBut= ActivePresentation.Slides(1).Shapes(1)
    ' bun.Fill.ForeColor.RGB = (RGB(255, 0, 0)) to confirm the button has been selected
    ' Add pulse effect to the shape
    Set effAdd = ActivePresentation.Slides(1).TimeLine _
    .MainSequence.AddTriggerEffect(pShape:=shp, _
    effectId:=msoAnimEffectFlicker, _
    trigger:=msoAnimTriggerOnShapeClick, _
    pTriggerShape:=trgBut)
    
End Sub

This is my second version of the code. It does not work at all because of the final pTriggerShape:=trgBut part. It returns:
Run-time error '-2147188160 (80048240)':
Sequence (unknown member) : Invalid request.
The problem is the pTriggerShape (I know because the yellow error arrow points to it) but I cannot move it because it is a mandatory parameter

Intended output:
shp has one more color pulse animation that is triggered with the previous animation in the trigger sequence.
i.e. the second animation (added by the script) is now after the first animation (added manually)  the second animation (added by the script) is now after the first animation (added manually)


Solution

  • I double checked your code and it should work, as per documentation, though there is no sample in the page. I integrated what I found here and it works.

    
    
    
    
    Sub AddAnim()
    ' Variables
    Dim sld As Slide: Set sld = ActivePresentation.Slides(4)
    Dim shp As Shape: Set shp = ActiveWindow.Selection.ShapeRange(1)
    Dim trgBut As Shape: Set trgBut = ActivePresentation.Slides(4).Shapes(1) ' Get selected shape
    
    With sld.TimeLine.InteractiveSequences.Add
       With .AddEffect(shp, msoAnimEffectFlicker, , msoAnimTriggerOnShapeClick)
          .Timing.TriggerShape = trgBut
       End With
    End With
    
    End Sub