Link to home
Start Free TrialLog in
Avatar of Steve Krile
Steve KrileFlag for United States of America

asked on

Programmatically set PowerPoint Motion Path animation

Is it possible to programmatically set a PowerPoint motion path?  I have a series of data (X,Y) that I would like to program in to an object's motion path so when someone views the slide, they will see the ball move around.  The data is being pulled from a database and placed in another slide in my presentation (although, that is not necessary - I can collect the data in vb and just set the motion path from there).  I tried recording a macro while creating a motion path on an object, but it appears as if the attributes for the motion path were not "written" during the recording process which makes me think this may not be possible.

Anyway, I'm looking for a quick and dirty way to have time-lapsed data presentation that is smooth.
Avatar of GlennaShaw
GlennaShaw

Here's a very long discussion on programming motion paths:
http://www.eggheadcafe.com/software/aspnet/28482014/-how-to-parse-a-string.aspx
Shyam also has some motion path add-ins that might work better for you:
http://skp.mvps.org/
Avatar of Steve Krile

ASKER

Woof...this is an interesting conversation, but I'm having trouble stripping out the relavent bits.  Seems like this is the important part:

Sub check_for_button(strMyFile As String)

    Dim oPresentation As Presentation
    Set oPresentation = Presentations.Open(strMyFile)
    Dim oSl As Slide
    Dim y As Long
    Dim i As Long
    Dim oSh As Shape
    Dim oEffect As Effect
    Dim oShB As Shape
    Dim pathdata(20)
    Dim aniMotion As AnimationBehavior
   
    With oPresentation
   
        For Each oSl In ActivePresentation.Slides
   
            ActiveWindow.View.GotoSlide (oSl.SlideIndex)
   
                With oSl.TimeLine
   
                    For i = .MainSequence.Count To 1 Step -1
   
                        If .MainSequence(i).Shape.Type = msoTextBox Then
                            Set shpAnswer = oSl.Shapes.AddShape(msoShapeRectangle, _
                            600, 50, 100, 50)
                            shpAnswer.TextFrame.TextRange.Text = "Answer"
                            GoTo Nextsub
                        End If
                    Next
   
                End With
   
Nextsub:        With oSl.TimeLine
                    y = 1
                    For i = .MainSequence.Count To 1 Step -1
                        If .MainSequence(i).Shape.Type = msoTextBox Then
                            Set oSh = .MainSequence(i).Shape
                            pathdata(y) = .MainSequence(i).Behaviors(1).MotionEffect.Path
                            Set oEffect = oSl.TimeLine.InteractiveSequences.Add.AddEffect(Shape:=oSh, effectId:=msoAnimEffectCustom, Trigger:=msoAnimTriggerOnShapeClick)
                           
                            With oEffect.Timing
                                .Duration = 2
                                .TriggerDelayTime = 0
                            End With
                           
                            Set aniMotion = oEffect.Behaviors.Add(msoAnimTypeMotion)
                           
                            With aniMotion.MotionEffect
                                .FromX = Split(pathdata(y))(1)
                                .FromY = Split(pathdata(y))(2)
                                .ToX = Split(pathdata(y))(4)
                                .ToY = Split(pathdata(y))(5)
                            End With
   
                            oEffect.Timing.TriggerShape = shpAnswer
   
                        End If
                    y = y + 1
                    Next i
                End With
            Next oSl
   
            oPresentation.Save
            oPresentation.Close
   
        End With
   
    Set oSh = Nothing
    Set oPresentation = Nothing
End Sub


But, i can't seem to get my head around this.  Could you help me with a simple example.  Say you have one slide and on that slide is one object.  How would you write a macro to move that object on a click for instance.  Forget about collecting the data.  Let's just hard code the coordinates for now.
If I jump on this boat, I'll be thinking in VB for days... (weird quirk I have, if I really start in programming I start thinking in the language and am barely fit to be around).

So instead, how about a 2 part tutorial that tells you all about how to program PowerPoint animation? :-)
http://msdn2.microsoft.com/en-us/library/aa168134(office.11).aspx
http://msdn2.microsoft.com/en-us/library/aa168135(office.11).aspx
And the specific VB reference for motion effects:
http://msdn2.microsoft.com/en-us/library/aa220971(office.11).aspx
(should also work with 2007)
These links are great, and I can sense it's where I need to be, but darnit if I'm not missing something simple.  I'm used to (from the world of Excel) putting a Sub in my project module, assigning that sub to an object's click event or whatever, and that's it.  However, that approach doesn't seem to be working in PowerPoint.  What very basic thing am I missing here?
Duh, sorry brain's slow.  Insert any object on the slide, then assign the Action Settings to run the macro.
Jusr right click on the object, click Action Settings...
Right.  So, did that.  Put a rectangle on it and set it's action to AddMotionPath().  Run I view the slide and click on the rectagle, a star flashes in the top corner then that's it.  I don't see it move, and clicking advances the slide.

Sub AddMotionPath()

    Dim shpNew As Shape
    Dim effNew As Effect
    Dim aniMotion As AnimationBehavior

    Set shpNew = ActivePresentation.Slides(1).Shapes.AddShape(Type:=msoShape5pointStar, Left:=0, Top:=0, Width:=100, Height:=100)
    Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence.AddEffect(Shape:=shpNew, effectId:=msoAnimEffectCustom, Trigger:=msoAnimTriggerWithPrevious)
    Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)

    With aniMotion.MotionEffect
        .FromX = 0
        .FromY = 0
        .ToX = 500
        .ToY = 500
    End With

End Sub

Is this a code problem, or am I implementing this incorrectly?  PP 2003 btw.
ASKER CERTIFIED SOLUTION
Avatar of GlennaShaw
GlennaShaw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wow.  Good to see the clarifications and this does answer my question...so, thanks for the links.
Here is the code that will work:

Sub AddMotionPath()

Dim shpNew As Shape
Dim effNew As Effect
Dim aniMotion As AnimationBehavior

Set shpNew = ActivePresentation.Slides(1).Shapes _
.AddShape(Type:=msoShape5pointStar, Left:=0, _
Top:=0, Width:=100, Height:=100)

Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
.AddEffect(Shape:=shpNew, effectId:=msoAnimEffectCustom, _
Trigger:=msoAnimTriggerWithPrevious)

Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)

effNew.Timing.Duration = 1
With aniMotion.MotionEffect
.FromX = 0
.FromY = 0
.ToX = 0.5
.ToY = 0.5
End With
   

End Sub
This is more along the line of what I was looking for:

[ Shapes("Oval 9") is an object on my first slide ]

This creates a nice curvy path for my oval to follow.

Sub AddMotionPath()

    Dim shpNew As Shape
    Dim effNew As Effect
    Dim aniMotion As AnimationBehavior
   
    Set shpNew = ActivePresentation.Slides(1).Shapes("Oval 9")
   
    Set effNew = ActivePresentation.Slides(1).TimeLine.MainSequence _
                .AddEffect(Shape:=shpNew, effectId:=msoAnimEffectCustom, _
                Trigger:=msoAnimTriggerWithPrevious)
   
    Set aniMotion = effNew.Behaviors.Add(msoAnimTypeMotion)
    effNew.Timing.Duration = 10
    With aniMotion.MotionEffect
       .Path = "M 4.72222E-6 5.18519E-6 " & _
                "C 0.00034 0.09052 0.00069 0.18126 0.03802 0.18889 " & _
                "C 0.07534 0.19653 0.22291 0.00393 0.22378 0.04607 " & _
                "C 0.22465 0.0882 0.03645 0.35232 0.04288 0.44121 " & _
                "C 0.0493 0.5301 0.15555 0.55464 0.2618 0.5794 E"
    End With
End Sub
I am soooo glad you got that, because those path coordinates are beyond me :-)
yeah, basically i don't know what they all mean...so, it's a trial and error thing  :)
Avatar of Jamie Garroch (MVP)
In case anyone else stumbles across this thread, this is briefly how the VML works:

1. There are three basic VML commands from the W3 standard which are supported by the PowerPoint animation engine and they are. M = MoveTo, L = LineTo, C = Curve. The E at the end is for, well, End!
2. The M and L commands take two arguments representing X and Y coordinates. But, and here's the clever gotcha, the coordinates are percentages of the slide dimensions. The reason is that a motion path with automatically scale when the parent shape is copy/pasted to a slide of a different size. Note too that the coordinates are relative to the centre of the shape the animation effect is added to, hence the VML path's starting command is nearly always "M 0 0". This example VML will move the shape half the width and height of the slide: "M 0 0 L 0.5 0.5 E" down and to the right. Why? Because 0,0 is at the top left of the slide.
3. The C command takes three pairs of X/Y coordinates to represent the two handles of the Bezier curve plus the point on the path.

And the reason I needed to understand this is that BrightCarbon (the company I work for) has added a new feature to its free BrightSlide add-in for PowerPoint that copies the outline of one shape and adds it as a motion path to a second shape. Adobe Illustrator users will love it!