Skip to content

Instantly share code, notes, and snippets.

@pcmoritz
Created May 20, 2018 19:04
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save pcmoritz/4b0e1be7f2dfcc4e51e2ace50426f67d to your computer and use it in GitHub Desktop.
Save pcmoritz/4b0e1be7f2dfcc4e51e2ace50426f67d to your computer and use it in GitHub Desktop.
Powerpoint create slides for animations while retaining slide numbers
Option Explicit
Sub AddElements()
Dim shp As Shape
Dim i As Integer, n As Integer
n = ActivePresentation.Slides.Count
For i = 1 To n
Dim s As Slide
Set s = ActivePresentation.Slides(i)
s.SlideShowTransition.Hidden = msoTrue
Dim max As Integer: max = AnimationElements(s)
Dim k As Integer, s2 As Slide
For k = 1 To max
Set s2 = s.Duplicate(1)
s2.Name = "AutoGenerated: " & s2.SlideID
s2.SlideShowTransition.Hidden = msoFalse
Dim oshp As Shape
With s2.Shapes
Set oshp = .AddTextbox(msoTextOrientationHorizontal, 10, 10, 100, 50)
oshp.TextFrame.TextRange.Font.Name = "Arial"
oshp.TextFrame.TextRange.Font.Size = 12
oshp.TextFrame.TextRange.InsertAfter "" & i
End With
s2.MoveTo ActivePresentation.Slides.Count
Dim i2 As Integer, h As Shape
Dim Del As New Collection
For i2 = s2.Shapes.Count To 1 Step -1
Set h = s2.Shapes(i2)
If Not IsVisible(s2, h, k) Then Del.Add h
Next
Dim j As Integer
For j = s.TimeLine.MainSequence.Count To 1 Step -1
s2.TimeLine.MainSequence.Item(1).Delete
Next
For j = Del.Count To 1 Step -1
Del(j).Delete
Del.Remove j
Next
Next
Next
End Sub
'is the shape on this slide visible at point this time step (1..n)
Function IsVisible(s As Slide, h As Shape, i As Integer) As Boolean
'first search for a start state
Dim e As Effect
IsVisible = True
For Each e In s.TimeLine.MainSequence
If e.Shape Is h Then
IsVisible = Not (e.Exit = msoFalse)
Exit For
End If
Next
'now run forward animating it
Dim n As Integer: n = 1
For Each e In s.TimeLine.MainSequence
If e.Timing.TriggerType = msoAnimTriggerOnPageClick Then n = n + 1
If n > i Then Exit For
If e.Shape Is h Then IsVisible = (e.Exit = msoFalse)
Next
End Function
'How many animation steps are there
'1 for a slide with no additional elements
Function AnimationElements(s As Slide) As Integer
AnimationElements = 1
Dim e As Effect
For Each e In s.TimeLine.MainSequence
If e.Timing.TriggerType = msoAnimTriggerOnPageClick Then
AnimationElements = AnimationElements + 1
End If
Next
End Function
Sub RemElements()
Dim i As Integer, n As Integer
Dim s As Slide
n = ActivePresentation.Slides.Count
For i = n To 1 Step -1
Set s = ActivePresentation.Slides(i)
If s.SlideShowTransition.Hidden = msoTrue Then
s.SlideShowTransition.Hidden = msoFalse
ElseIf Left$(s.Name, 13) = "AutoGenerated" Then
s.Delete
End If
Next
End Sub
@AntoineLu
Copy link

Awesome thank you!

@dvirsegal
Copy link

Thank you , very helpful!

@srashtigoel
Copy link

Thank you!! Very helpful!!

@jenel1987
Copy link

thank you. however, this only works for animation sequence set on click. It doesn't work on such animation that starts with previous. Is it possible to code that as well?

@sunrset
Copy link

sunrset commented Oct 4, 2020

Thanks!

@lafnian1990
Copy link

Thanks!

@finistratbob
Copy link

finistratbob commented May 13, 2021

This is good advice. It seems to me that a professional person should be able to create and customize animations for presentations. I support creative coding, but it seems to me that sometimes ready-made solutions are needed. I ordered https://masterbundles.com/downloads/disney-powerpoint-template-2020-50-unique-slides/ a pc of cool presentation themes with animated elements. It seems to me that this saves time on coding unique elements. The main task is to create a beautiful visualization.

@bosheng0701
Copy link

thanks !~

@oura71
Copy link

oura71 commented Sep 30, 2022

Very useful! thank you very much!

@aiskhak
Copy link

aiskhak commented Jun 8, 2023

thank you. however, this only works for animation sequence set on click. It doesn't work on such animation that starts with previous. Is it possible to code that as well?

Yes, much desired feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment