Knowledge.ToString()

2 Ways to Add Custom Path Animation Effect Using VBA in PowerPoint 2013

As Microsoft got rid of “Record Macro” button from PowerPoint 2013, it is extremely difficult to write a macro for PowerPoint which does what we want to do. Recently, I had a need for having an animation with custom path for a shape. I tried all possible solutions available on the internet and could not get what I wanted. I found two ways to add custom path animation effect for a shape

Download Sample PowerPoint

Method 1: Use following VBA Code

Sub AddStarNoVisiblePath()
	' Note: Change slide index if needed
	' Variables
	Dim shpNew As Shape
	Dim effNew As Effect
	Dim aniMotion As AnimationBehavior
	 
	' Add a new STAR shape at a specified location
	Set shpNew = ActivePresentation.Slides(2).Shapes _
	.AddShape(Type:=msoShape5pointStar, Left:=100, _
	Top:=100, Width:=100, Height:=100)
	 
	' Add custom effect to the shape
	Set effNew = ActivePresentation.Slides(2).TimeLine.MainSequence _
	.AddEffect(Shape:=shpNew, effectId:=msoAnimEffectCustom, _
	Trigger:=msoAnimTriggerWithPrevious)
	 
	' Add Motion effect
	Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
	effNew.Exit = msoFalse
	 
	' Set Motion Path as square path
	aniMotion.MotionEffect.Path = "M 0 0 L 0.25 0 L 0.25 0.25 L 0 0.25 L 0 0 Z"
 
End Sub

Explanation

Add a Star shape, create new custom animation effect, set animation as motion and set the motion path

Pros

You can use this trick to create a PowerPoint from scratch without any dependency.

Cons

The path is not visible when the shape is selected (Read further down for technical explanation) so you cannot make a change through UI because the animation is marked as “Custom” Entry animation and not “Custom Path” animation as shown in the image below. You must use VBA code to make the change. It makes the maintenance very difficult as you loose the visual clue of the path.

powerpoint no visible animation path
No visible animation path when shape is selected but works

Method 2: Copy existing shape with custom path animation and change path

Prerequisite

You must have the shape with Custom Path animation already set regardless of the path. You may have that shape in the same PowerPoint or in a separate PowerPoint presentation file.

Code

Sub CopySmileyChangePath()
	' NOTE: Change slide and shape index based on where/when you are executing this macro
	' 3rd shape on slide 2 is smiley which has a custom path already defined
	ActivePresentation.Slides(3).Shapes(3).Copy
	 
	' Paste shape in slide 1
	With ActivePresentation.Slides(2).Shapes.Paste
	.Name = "Smiley-" & ActivePresentation.Slides(2).Shapes.Count
	.Left = 200
	.Top = 200
	End With
	 
	' Note: Chnage main sequence index if needed
	' Change shape path to triange
	ActivePresentation.Slides(2).TimeLine.MainSequence(2).Behaviors(1).MotionEffect.Path = "M 0 0 L 0.125 0.216 L -0.125 0.216 L 0 0 Z"
	 
End Sub

Explanation

Copy the desired shape, paste at desired location (pasting shape automatically pastes animation) and change the path

Pros

When the shape is selected, path is visible which gives a visual clue to the user. You can easily change the path if you need to modify it.

Visible animation path in PowerPoint
Visible animation path in PowerPoint

Cons

It requires dependency on existing shape with “Custom Path” animation in the same file or in any other file. Sometimes you may not have this liberty.

Why path is not visible in Method 1?

When we use following code, it sets the animation effect as Entry or Exit effect. Default value is msoFalse ( = Entry effect)

effNew.Exit = msoFalse

Microsoft came up with Entry, Exit, Emphasis and Path effects. Out of these four, Entry and Exit can be specified using “Exit” property and Emphasis effect may be derived from the EffectType (not verified by me) but Path effect is the one which cannot be explicitly set using the VBA code. So PowerPoint considers it as an Entry/Exit effect and hence path is not visible.

Hint for developer

effNew.Exit property is setting the value of OpenXml PresetClass which is always “Entry” or “Exit” but not “Path”. If any of the method does not work for you, you can always code in OpenXml and get the desired result.

Share

Comments

3 responses to “2 Ways to Add Custom Path Animation Effect Using VBA in PowerPoint 2013”

  1. Sanjay Avatar
    Sanjay

    Excellent

  2. RC Avatar
    RC

    Thank you for the sample code! I found a way to make the path visible/editable for Method 1. When effNew is created, it has one (placeholder?) behavior, Type:= msoAnimTypeSet. As the last step in defining the animation, I deleted that behavior. It is still shown as a “Custom Animation”, but the path can be edited. I don’t know how well that works in general.

    1. Mike Avatar
      Mike

      This tweak is great. It solves another other important “unwanted result” that happened to me.
      Situation: Shape1 and Shape2. Shape1 is animated first, by means of ordinary means (the control bar); Shape2 is animated secondin the timeline, and by means of the code in this page.

      What happened to me: Shape1 animated, then Shape2 APPEARED (which I did not want), then Shape2 animated.

      What happens now, by removing the “hidden behavior”:
      Shape1 animates, Shape2 (which is already visible) animates.

      This post is old but useful (thanks to Vishal). It would be nice to have it (also) on Stackoverflow, so that people using it can be identified by means of an account and, just in case, getting in touch with each other.

Leave a Reply

Your email address will not be published. Required fields are marked *