Is it possible to trigger a shape animation from VBA code?
pom
Pom,
In any version less than powerpoint 2010, no it does not appear possible: http://www.pptalchemy.co.uk/vba_Triggers.html.
However, if you are using powerpoint 2010, MSDN details a straightforward demo for triggering shape animations in powerpoint:
Sub TestShapeAnimation()
With ActivePresentation.Slides(1)
Dim shp1, shp2, shp3 As Shape
' This sets the initial shape, with which we will test the animation sequences.
Set shp1 = .Shapes.AddShape(msoShape12pointStar, 20, 20, 100, 100)
' This creates the animations.
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectFadedSwivel, , msoAnimTriggerAfterPrevious
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectPathBounceRight, , msoAnimTriggerAfterPrevious
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectSpin, , msoAnimTriggerAfterPrevious
' This acquires the animation... [i]
shp1.PickupAnimation
' [i] ... and applies it to another shape.
Set shp2 = .Shapes.AddShape(msoShapeHexagon, 100, 20, 100, 100)
shp2.ApplyAnimation
' Another shape creation / animation application.
Set shp3 = .Shapes.AddShape(msoShapeCloud, 180, 20, 100, 100)
shp3.ApplyAnimation
End With
End Sub
Let me know if you have further questions/thoughts -
~JOL
JackOrangeLantern's response is unfortunately not especially responsive to the original question.
JOL's response will help you copy animations on the timeline and apply them to different shapes, but it will not actually trigger them.
If you have Powerpoint 2010 or earlier, there is no direct way to actually trigger an animation with VBA. I am not very familiar with the newer versions of Powerpoint, but I don't think they have this feature either.
Actually triggering interactive animations seems to require an actual mouse click which can not be easily simulated in Powerpoint without some advanced and overly complicated coding that might involve actually messing with the Windows API. The best workaround I have found for this is to not send mouse clicks to the shapes on the slide but to instead send keyboard strokes.
I discovered this solution from PPT Alchemy Mouseover trigger an animation with vba code
Sub anim()
SendKeys("{TAB}")
SendKeys("{ENTER}")
End Sub
This code will activate the first interactive Trigger On the slide. To activate the second Trigger animation on the slide you would send the {TAB} key twice instead of once. This is definately the easiest way to activate trigger animations on a powerpoint slide. It is a shame there isn't a better way.
Follow the link for a more thorough explanation.
Take care. :)
Related
i'am currently trying to add a small function to PowerPoint using VBA, my goal is to create a gadget that works kind of like Photoshop graphics layer.
My plan is to add a layer name Tag on each shape that is drawn by user, so later I can parse through every item by loop and preform lock/unlock, show/hide shapes based on it's tag value, such as:
Sub add_shape_with_layer_tag()
Set islide = ActivePresentation.Slides(1)
Set ishape = islide.Shapes.AddShape(msoShapeRectangle, 5, 5, 80, 60)
ishape.Tags.Add "Layer", "1"
End Sub
Sub show_hide_layer_one_shapes()
Dim active_slide As Slide
Set active_slide = ActiveWindow.View.Slide
For Each ishape In active_slide.Shapes
If ishape.Tags("Layer") = "1" Then
ishape.Visible = Not (ishape.Visible)
End If
Next ishape
End Sub
However, I couldn't found a way to achieve this, so I would like to ask whether there's a method that provides following functions?
override add shape function so I can sneak tag value in to shape every time user drawn a shape
catch add shape event (if this thing did exist) so i can add tag to the last added item
a way to set a default tag value to shape, just like setting default shape color/line width
or is there any better options that is also viable?
thanks.
20200728
To John, thanks for the advice, I did found some interesting event that might be helpful for some of my other projects, however I couldn't found events that able to trigger after custom function while shape is added.
To Steve, my plan is to add a modeless userform with list UI that manage layers, the shape tag and shape fill texture/color will be determined based on what list item that currently selected.
As to saving settings, I'll use VBComponents.CodeModule to dump existing settings in userforms to a VBA module and store as text, so in theory I should able to make this function self contained in one file.
Not totally an answer, sorry SO, but comments don't allow enough scope for this. So ...
To Steve, my plan is to add a modeless userform with list UI that manage layers, the shape tag and shape fill texture/color will be determined based on what list item that currently selected.
Ah, so you're creating your own "pseudo-layers". That wasn't clear. Thanks for the add'l info. As it happens, I have a selection manager add-in that works very similarly to what you're proposing.
To John, thanks for the advice, I did found some interesting event that might be helpful for some of my other projects, however I couldn't found events that able to trigger after custom function while shape is added.
The SelectionChange event should get you there. When it fires, you'll need to check first to see if the current selection is a shape or something else. If a shape, check to see if its .Index = current slide's .Shapes.Count and also to see if you've already tagged it. If no tag AND if it's the correct index, it'll be a newly added shape. If you're tagging ALL shapes, you may only need to check to see if the .Tag(name) is blank.
As to saving settings, I'll use VBComponents.CodeModule to dump existing settings in userforms to a VBA module and store as text, so in theory I should able to make this function self contained in one file.
Why not save any needed slide or presentation level info as further slide- or presentation-level tags? PPT can absorb quite a lot of info as tags w/o getting cranky.
In short, the issue I'm running into is that when using VBA to change font size during a presentation, the slide runs all animations for that slide a second time. Changing other qualities, such as font color or shape fill, the slide does not animate a second time.
The longer version: I'm creating a tool in PowerPoint for my trainers to be able to build quizzes and computer-based trainings. I'm using a template shape for each state of the button (correct, incorrect or unanswered), and then have .pickup and .apply to change the formatting of the shapes on the question slides. At runtime, the slide builds based on the animations, the user clicks an option and the shapes format immediately. However, the font size also changes to mirror the template shape. When I temporarily store the font size and reapply it after .apply, at runtime, the slide animates, the user clicks an option and the slide runs the animations again (albeit correctly with the new formatting). The code works, but certain changes cause the slide to animate a second time (text, size), and some changes don't (shape color, text color). It's this second animation of the slide I need to avoid.
Windows 7 Professional;
PowerPoint 2016
Instead of using .pickup & .apply, one thought was to change qualities of everything but the font size, (the fill and line qualities), but for the fill, alone there are 20+ qualities, and that's before getting into number of gradient stops, shadows, etc. I need my developers to have the freedom of formatting the shape as they want that my code can quickly pull from.
.SlideShowSettings.ShowWithAnimation - setting to 'false' at the start of the code and 'true' yielded no change in results. (Attempt shown in code below.)
.SlideShowWindow.View.State - setting to 'paused' and 'running' also did not affect anything. (Attempt shown in code below.)
All objects on the slide are set to fade in automatically upon showing the slide. The simplified code here is set to run when a shape is clicked and shows what works and what doesn't work. I've commented lines in and out so I can try different things.
Sub ProcessResponse()
'These options do not prevent re-animation when in mid-presentation:
'ActivePresentation.SlideShowSettings.ShowWithAnimation = msoFalse
'ActivePresentation.SlideShowWindow.View.State = ppSlideShowPaused
'The following items do NOT cause the slide to run the animation again (desired result):
'Changing the object fill.
'ActivePresentation.Slides(1).Shapes(1).Fill.ForeColor.RGB = RGB(50, 25, 100)
'Changing text color.
'ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Font.Color.RGB = RGB(50, 25, 150)
'The following lines of code cause the slide to run the animation again (undesired result):
'Changing the font size.
ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Font.Size = 40
'Changing the text.
'ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text = "Changed"
'Closing the above attempts.
'ActivePresentation.SlideShowSettings.ShowWithAnimation = msoTrue
'ActivePresentation.SlideShowWindow.View.State = ppSlideShowRunning
End Sub
Setup: Slide with animations (i.e., fade in) that run "after next". 3-4 shapes, each with the above code called when clicked.
During the slide show:
Expected - animations, user clicks shape, the changes are made, no further actions/animations.
Actual Result - animations, user clicks shape, animations (changes are included).
Thank you for any and all help and considerations. Your time is appreciated.
Does anyone know how to ungroup SmartArt element via VBA?
Sub UngroupSmartArt()
Dim shapeWithSmartArt As Shape
Set shapeWithSmartArt = ActivePresentation.Slides(2).Shapes(2)
shapeWithSmartArt.Ungroup
End Sub
I get an error for this code:
"This member can only be accessed for a group."
It doesn't make any sense to me, because it's easily possible to do it in powerpoint itself (Right click on SmartArt -> Group -> Ungroup). It's driving me nuts :)
Can anyone help me with ungrouping SmartArt element/shape?
I also took a look on similar question, but it doesn't work properly, because ungrouped result is different in comparison to the one made via powerpoint itself.
Please help me out. I would really appreciate any help!
It is simply impossible to do it via VB code. That is also statement from Microsoft. Disadvantage of using SmartArt is also that user cannot record any actions with macro (using Excel) which are performed on this type of object/element.
It is also impossible to change width or height property of SmartArt nodes via VB, this was actually the reason why I wanted to change SmartArt element to shapes, because you can easily change width and height property of the shape via code.
Microsoft and their developers should really consider to make SmarArt element more developer-friendly, because I noticed I'm not the only one with these issues.
EDIT: Solution found! There is a way to execute commands from the powerpoint ribbon. You need to select your shape first, afterwards execute CommandBars.ExecuteMso with the action: SmartArtConvertToShapes.
Sub UngroupSmartArt()
Dim shapeWithSmartArt As Shape
Set shapeWithSmartArt = ActivePresentation.Slides(2).Shapes(2)
shapeWithSmartArt.Select
CommandBars.ExecuteMso("SmartArtConvertToShapes")
End Sub
This still doesn't change the fact and my point of view: Microsoft should really consider to make SmartArt element more friendly to developers!
I'm developing a PowerPoint2007 Add-on using VSTO(Visual Studio Tools for Office)
I'm trying to add a wavfile to slide and modify animation setting of the wave file.
The problem is occurred when I modify AnimationSettings.PlaySettings's member attributes of the wave file shape then some of animation effect in the slide deleted.
This behavior is very weird.
I don't understand why some of animation effect has been deleted after I modify the PlaySettings's member attributes.
code :
Shape s = Globals.ThisAddIn.Application.ActivePresentation.Slides[slideIndex].Shapes.AddMediaObject(wavFilePath, 0f, 0f);
s.Left = DEFAULT_LEFT_POS;
// If the below code run, some of animation effect are deleted.
s.AnimationSettings.Animate = Microsoft.Office.Core.MsoTriState.msoTrue;
s.AnimationSettings.AdvanceMode = PpAdvanceMode.ppAdvanceOnTime;
s.AnimationSettings.AnimationOrder = 0;
s.AnimationSettings.PlaySettings.PauseAnimation = Microsoft.Office.Core.MsoTriState.msoFalse;
s.AnimationSettings.PlaySettings.PlayOnEntry = Microsoft.Office.Core.MsoTriState.msoTrue;
Can anyone help me?
According to my experiences using AnimationSettings delete all 'exit effects' of other shapes. What you could possibly try is to explore the following PowerPoint objects/references:
`(slide).TimeLine.MainSequence`
which allows to change the animation order (you could add sound shape and than move it up before any exit effect start).
Check also if (Shape).SoundEffect object is not a good substitution for your needs.
I also change tag of your question into powerpoint-vba as it has a reference to that area too.
Is it possible to identify the shape ID that triggers a mouseover on a powerpoint presentation.
What I want to do is have a series of shapes on a slide with titles, and when the user mouseovers a shape, I want to use VBA to show more details about the shape in another part of the page, possibly in a separate help text box. I could then use the selected shapes ALt Text to display in the help box. What i can't see is how to identify what shape has triggered the mouseover macro. Is this possible? If I can identify the shape that triggered the action, I could then get info about that shape.
The Shape object in PowerPoint does not allow you to interact with its events as you normally would with other objects in VBA code. The only two 'events' that are exposed are not actually VBA events. They are a special class called Actions, and they are 'ppMouseOver' and 'ppMouseClick'. If you know all the information about your shapes and their corresponding information beforehand, you could write some code to do what you want, but it would all be hard-coded, which is probably not what you want.
You could run a routine something like this when the presentation starts:
Sub SetActionsRoutine()
Shape1.ActionSettings(ppMouseOver).Action = ppActionRunMacro
Shape1.ActionSettings(ppMouseOver).Run = "showInformation1"
Shape2.ActionSettings(ppMouseOver).Action = ppActionRunMacro
Shape2.ActionSettings(ppMouseOver).Run = "showInformation2"
...
...
End Sub
And then whenever you did a MouseOver of those shapes, one of the following routines would run.
Sub showInformation1()
myTextBox.Text = Shape1.AlternativeText
End Sub
Sub showInformation2()
myTextBox.Text = Shape2.AlternativeText
End Sub
This is very limited and requires that you write a subroutine for every shape on your slide. Probably not the approach you want, but again, with PowerPoint, your options are very limited.
New to StackOverflow, late to answer, but you can do this:
Sub RespondToShape(oSh as Shape)
MsgBox "You clicked " & oSh.Name
End Sub
Assign every shape you want to interact with an Action setting of Run Macro and choose RepondToShape as the macro.