Please Heeeeelp! Vba code to add animation to an existing object

L

lizsantiago

I am a teacher trying to make a game using 2007, and i am trying to do two
things. One is to add an animation like the spin to a picture by clicking on
a botton, but all the examples i find , add the animations while creating a
shape so i dont know how to make the animation to an existing picture in my
slide. the other thing is to stop the animation by clicking on a button. I
found an example to pause the slide but the problem is that i have other
animations that are stopped as well and i cant use that. The picture is of a
wheel with numbers that i want to spin so when i click the stop it stops on a
different number of the wheel. for the spin i used the animation set to
..01 speed until slide ends and used the pause botton to stop it and that
works great to make it stop in a random number but as i said it stops the
textboxes that should appear with the questions for the students. any aideas
on how to use a code to do what i want. i have bought 3 books and read a
thousand sites, posted in every forum i have found but no one care to answer.
this is my last resort.
 
D

David Marcovitz

I am a teacher trying to make a game using 2007, and i am trying to do two
things. One is to add an animation like the spin to a picture by clicking on
a botton, but all the examples i find , add the animations while creating a
shape so i dont know how to make the animation to an existing picture in my
slide. the other thing is to stop the animation by clicking on a button. I
found an example to pause the slide but the problem is that i have other
animations that are stopped as well and i cant use that. The picture is of a
wheel with numbers that i want to spin so when i click the stop it stops on a
different number of the wheel. for the spin i used the animation set to
.01 speed until slide ends and used the pause botton to stop it and that
works great to make it stop in a random number but as i said it stops the
textboxes that should appear with the questions for the students. any aideas
on how to use a code to do what i want. i have bought 3 books and read a
thousand sites, posted in every forum i have found but no one care to answer.
this is my last resort.

If you want to set an animation to an existing shape, you have to
identify the shape. This can be done by shape name or number (names tend
to be more stable). For example,

ActivePresentation.Slides(3).Shapes(5)

is a pointer to the 5th shape on slide 3. If you name that shape, then
you could use the name. For example if you named it My Pretty Shape, you
could get to the shape with:

ActivePresentation.Slides(3).Shapes("My Pretty Shape")

Whatever code you have that does something to a slide that is being
added should then work with this. If you post a snippet, we can help you
get it to work properly.

--David

--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
L

liz santiago

Thanks David, I know how to name the object that i am using since i bought your book, which by the way have made me get interested in vba, my real problem is that i dont know how to assign to a shape one of the effects like spin or fade or something like that. For now i am using this
Sub RndSpin(oShp as Shape)
Dim t As Single
t = Timer + (Rnd * 4) + 1
Do Until Timer > t
oShp.Rotation = oShp.Rotation + 5
DoEvents
Loop
End Sub

but the spinning is too slow, though it has the exact effect that i need because it lands in a random position of the wheel.
 
D

David Marcovitz

Great. I'm glad you are learning from the book. Assigning animation with
VBA is a bit tricky and then using VBA to activate it is trickier still.
As I was looking for a link to Shyam's site with explanation of this, I
came across this post by him:
You can assign an animation in PPT
2002 and later using the following code:
Sub CreateAnimation()
Dim oEffect As Effect
Dim oShpA As Shape
With ActivePresentation.Slides(1)
'Create two autoshapes on the slide.
Set oShpA = .Shapes.AddShape(msoShapeRectangle, 100, 100, 50, 50)
' Assign an animation to shape A
Set oEffect = .TimeLine.MainSequence.AddEffect(Shape:=oShpA,
effectId:=msoAnimEffectAppear)
End With
End Sub


To assign an interactive animation take a look at the code here:
http://skp.mvps.org/pptxp012.htm#interactive

You can turn off the animations on the slides but this applies to the
presentation as a whole and not specific slides. Look at the
slideshowsetting - ShowWithAnimation property to address this.

To determine the animation status you need to setup an event hook to track
the animations as they fire. Look on my site for a downloadable example of
eventhandler in PowerPoint http://skp.mvps.org/download.htm


In this example, you already have the shape so you don't need to create
the shapes or assign it to oShpA. You can skip that part of the code and
just use the oShp that you already have.

--David

Thanks David, I know how to name the object that i am using since i bought your book, which by the way have made me get interested in vba, my real problem is that i dont know how to assign to a shape one of the effects like spin or fade or something like that. For now i am using this
Sub RndSpin(oShp as Shape)
Dim t As Single
t = Timer + (Rnd * 4) + 1
Do Until Timer> t
oShp.Rotation = oShp.Rotation + 5
DoEvents
Loop
End Sub

but the spinning is too slow, though it has the exact effect that i need because it lands in a random position of the wheel.


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
L

lizsantiago

Thank you again for your time, i am trying this but it doesnt work
Sub CreateAnimation()
Dim oEffect As Effect

With ActivePresentation.Slides(1).Shapes("Picture 3")
Set oEffect = .TimeLine.MainSequence.AddEffect(Shape:="Picture 3",
effectId:=msoAnimEffectSpin)
End With
End Sub
It says method or data member not found while highlitghing TimeLine. I have
no idea what is that.

another thing is there a way to make the rotation in this one to go faster?
Sub RndSpin(oShp as Shape)Thanx thanx thanx !!
 
L

lizsantiago

Well any other suggestion would be appreciated... i think i do need a code
because what i want is to make a wheel with numbers spins and stops at a
random place for a game and i dont want to use a long list of animations to
make it stop at different points that at the end and after playing the game a
couple of time with my students they will know what number would come next.
besides that i want to be able to add animations to shapes that i already
have and all the examples i have seen (and believe me i have seen thousands)
they all add the effects while creating the shape like the one posted in
here, and when i try to apply it to my shapes( pictures) they never work (of
course i am just learning vba). So if you know a code that i can use to do
this i will be in debt with you my whole life!! i am a teacher and 90% of my
class is about games in powerpoint.
 
D

David Marcovitz

That's because the Timeline is part of the Slide, not the Shape. If your
shape is oShp, then you need something like (I haven't tested this)

With ActivePresentation.Slides(1)
Set oEffect = .TimeLine.MainSequence.AddEffect(shape:=oShp, effectId:=
msoAnimEffectSpin)
End With

If you haven't already, then you probably need to set oShp to be the
right thing before this:

Set oShp = ActivePresentation.Slides(1).Shapes("Picture 3")

--David

Thank you again for your time, i am trying this but it doesnt work
Sub CreateAnimation()
Dim oEffect As Effect

With ActivePresentation.Slides(1).Shapes("Picture 3")
Set oEffect = .TimeLine.MainSequence.AddEffect(Shape:="Picture 3",
effectId:=msoAnimEffectSpin)
End With
End Sub
It says method or data member not found while highlitghing TimeLine. I have
no idea what is that.

another thing is there a way to make the rotation in this one to go faster?
Sub RndSpin(oShp as Shape)
Thanx thanx thanx !!


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
S

Shyam Pillai

Look the at required argument types. AddEffect is expecting a shape
reference and not the name of the shape.

Sub CreateAnimationS()
Dim oEffect As Effect

With ActivePresentation.Slides(1)
Set oEffect =
..TimeLine.MainSequence.AddEffect(Shape:=.Shapes("Picture 3"),
effectId:=msoAnimEffectSpin)
End With
End Sub

Regards,
Shyam Pillai

Image Importer Wizard: http://skp.mvps.org/iiw.htm
 
L

liz santiago

OHHH! that works but just to make the animation not for what i need.. i was looking to make a button to add the macro and when i click on the button the spin started but that code just create the animation the same as do it directly on the animation tab. so let me try again, cuse there has to be away to add an animation to a shape without creating the shape so it's trigger by clicking on another shape. if not how can i add more speed to the code i posted before that makes the picture rotate?, that works fine but it is too slow.
 
J

John Wilson

Did you look at the example in the link?

lizsantiago said:
Well any other suggestion would be appreciated... i think i do need a code
because what i want is to make a wheel with numbers spins and stops at a
random place for a game and i dont want to use a long list of animations
to
make it stop at different points that at the end and after playing the
game a
couple of time with my students they will know what number would come
next.
besides that i want to be able to add animations to shapes that i already
have and all the examples i have seen (and believe me i have seen
thousands)
they all add the effects while creating the shape like the one posted in
here, and when i try to apply it to my shapes( pictures) they never work
(of
course i am just learning vba). So if you know a code that i can use to do
this i will be in debt with you my whole life!! i am a teacher and 90% of
my
class is about games in powerpoint.



__________ Information from ESET Smart Security, version of virus
signature database 4995 (20100402) __________

The message was checked by ESET Smart Security.

http://www.eset.com

__________ Information from ESET Smart Security, version of virus signature database 4995 (20100402) __________

The message was checked by ESET Smart Security.

http://www.eset.com
 
D

diane

Liz, you don’t need to create a macro or use VBA to create a spinner that a
viewer can randomly stop. John’s solution (following his link to find
“PowerPoint Spinnerâ€) is a very easy and elegant solution. It’s simply using
a trigger to initiate and stop the spin of a clipart of a numbered wheel. You
would want to use triggers to stop the spinning because, as you’ve noted,
using slide transitions to stop the animation just moves you to the next
slide. If you're not familiar with creating triggers, on John's link, look
for Triggers on the left hand side
 
L

liz santiago

john there is so many post that i am kind of lost here, what link? actually i am not even sure if you were the one who asked me if i saw the link.
 
L

lizsantiago

you wont beleive it but i am still looking to achieve this game, i saw the link which directed me to a spinnner but that is for sell, is that made with animations only? cuse i have try all animations in the panel and cant till find one that stop the wheel in different points randomly, i can only stop it at points set with the rotation degree and that is not random cuse it will only stop at those points and after playing a few time my students will know what comes next, unless i redo all the animations again.
 
D

David Marcovitz

you wont beleive it but i am still looking to achieve this game, i saw the link which directed me to a spinnner but that is for sell, is that made with animations only? cuse i have try all animations in the panel and cant till find one that stop the wheel in different points randomly, i can only stop it at points set with the rotation degree and that is not random cuse it will only stop at those points and after playing a few time my students will know what comes next, unless i redo all the animations again.


What if we take a different approach. I am imagining two possibilities:

(1) Have one shape and use VBA to adjust the rotation.
(2) Have multiple shapes drawn at different rotations and use VBA to
cycle through which shape is showing at which time.

Either way, you can use generate a random number to decide how many
times to rotate the shape and or cycle through the pictures. It might
not be quite as smooth as an animation, but it shouldn't be too hard and
should do the trick.

--David

--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
D

David Marcovitz

What if we take a different approach. I am imagining two possibilities:

(1) Have one shape and use VBA to adjust the rotation.
(2) Have multiple shapes drawn at different rotations and use VBA to
cycle through which shape is showing at which time.

Either way, you can use generate a random number to decide how many
times to rotate the shape and or cycle through the pictures. It might
not be quite as smooth as an animation, but it shouldn't be too hard and
should do the trick.

--David

I played with this a little bit. I can't get it to go fast, but this
code does the spinning. After this, you just need to check the rotation
of the object to calculate what number it landed on:

Sub Spin()
Dim spinNumber As Long
Dim i As Long

Randomize
spinNumber = 360 * Rnd
For i = 1 To spinNumber
ActivePresentation.Slides(1).Shapes(2).IncrementRotation (1)
'Sleep 1
ActivePresentation.SlideShowWindow.View.GotoSlide 1
Next i

End Sub


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
L

lizsantiago

I will try it tomorrow and post how it went, i want to thank you and all the people here who have been trying to help me.
 
D

David Marcovitz

I can't remember if I was trying this in 2003 or 2007 (I think 2003),
but I was having problems with the speed. It seemed that no matter what
I did, every rotation would take about a second. I tried Sleep , and
that just slowed it down more, which is why I dropped it from my code.
And when I left out the GotoSlide, it wouldn't refresh at all and just
pause and then randomly show up with something rotated. Initially, I
imagined this spinning around several times in a few seconds, but I cut
it down to a maximum rotation of 1 time around so it wouldn't take forever.
--David

Your PPT 2007 visibility bug or its cousin?

I started with your code and modified it a wee bit ... for all intents
and purps it's identical, just a bit more generic. This lets me assign
the macro as an action setting on the shape so it acts when clicked on.
Works nicely in 2003 ... haven't tried it in
bugfest^H^H^H^H^H^H^H2007.

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub Spin(oSh As Shape)
Dim spinNumber As Long
Dim i As Long

Randomize
spinNumber = 360 * Rnd
For i = 1 To spinNumber
oSh.IncrementRotation (1)
Sleep 1
ActivePresentation.SlideShowWindow.View.GotoSlide
oSh.Parent.SlideIndex
Next i

End Sub



==============================
PPT Frequently Asked Questions
http://www.pptfaq.com/

PPTools add-ins for PowerPoint
http://www.pptools.com/


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
L

lizsantiago

David, thanks let me tell you that your code works is a pity i cant make it go faster. Do you think this could be mix wioth your code to make it faster
Returns or sets a Single that represents the speed, in seconds, of the specified animation. Read/write
Synta

expression.Spee

expression A variable that represents a Timing object

Exampl

This example sets the animation for the main sequence to reverse and sets the speed to one second

Visual Basic for Applications
Sub AnimPoints(
Dim tmlAnim As TimeLin
Dim spdAnim As Timin

Set tmlAnim = ActivePresentation.Slides(1).TimeLin
Set spdAnim = tlnAnim.MainSequence(1).Timin
With spdAni
.AutoReverse = msoTru
.Speed =
End Wit
End Sub

Steve, i couldn't make your code work but thanks for all your replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top