Consulting

Results 1 to 10 of 10

Thread: How to reliably change the fill colour of a rectangle using an animation timeline

  1. #1

    How to reliably change the fill colour of a rectangle using an animation timeline

    I am coding using PowerPoint VBA, but am having difficulties trying to reliably change the fill colour of a shape (rectangle) using an animation timeline and msoAnimEffectChangeFillColor. What happens is on the first occasion the colour-change works as intended, but doing a repetition on the same shape (normally the second or third run) results in the replacement colour changing to a different undesired colour (normally orange - I believe this is the default colour set by Powerpoint?) I have found this easy to replicate and give an example with code below.
    If you create a PowerPoint and insert a filled rectangle and run the below macro (such as when the rectangle is clicked on) - the first click changes to the intended colour (here red). However, if you keep running the macro the colour changes to orange (normally on the third click!) I would be grateful if anyone could explain why this happens and any suggestions for a solution! The other observation I have made is that often I have to reset the PowerPoint in the Visual Basic Editor for the animation to start working again so it fills the rectangle with the intended (red) colour!

    Dim oshp As Shape
    Dim oeff As Effect
    Dim MyDocument As Slide
    
    
    Sub rectangle()
    
    
    'This text is not part of the animation but removes any existing animations on the current timeline'
    Dim i As Integer
    For i = ActivePresentation.Slides(1).TimeLine.MainSequence.Count To 1 Step -1
    ActivePresentation.Slides(1).TimeLine.MainSequence(1).Delete
    Next i
    
    
    'This is the code to create the animation.
    Set MyDocument = ActivePresentation.Slides(1)
    Set oshp = MyDocument.Shapes("Rectangle 3")
    Set oeff = MyDocument.TimeLine.MainSequence.AddEffect _
    (Shape:=oshp, effectid:=msoAnimEffectChangeFillColor, trigger:=msoAnimTriggerWithPrevious)
    oeff.EffectParameters.Color2.RGB = RGB(255, 0, 0)
    oeff.Timing.Duration = 0.25
    oeff.Timing.TriggerDelayTime = 0.5
    
    
    End Sub


    Thank you for any comments or suggestions!

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Try declaring the variables (especially oeff) INSIDE the Sub - Don't ask why I don't know!

    Sub rectangle()
    Dim oshp As Shape
    Dim oeff As Effect
    Dim MyDocument As Slide
    
    
    'This text is not part of the animation but removes any existing animations on the current timeline'
    Dim i As Integer
    For i = ActivePresentation.Slides(1).TimeLine.MainSequence.Count To 1 Step -1
    ActivePresentation.Slides(1).TimeLine.MainSequence(1).Delete
    Next i
    
    
    'This is the code to create the animation.
    Set MyDocument = ActivePresentation.Slides(1)
    Set oshp = MyDocument.Shapes("Rectangle 3")
    Set oeff = MyDocument.TimeLine.MainSequence.AddEffect _
    (Shape:=oshp, effectid:=msoAnimEffectChangeFillColor, trigger:=msoAnimTriggerWithPrevious)
    oeff.EffectParameters.Color2.RGB = RGB(255, 0, 0)
    oeff.Timing.Duration = 0.25
    oeff.Timing.TriggerDelayTime = 0.5
    
    
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    John Wilson - thank you very much - my code now works! I am a Teacher and have only been coding with VBA PowerPoint for two weeks so really appreciate your help. This also solved my last issue concerning a program I have been making over this time period and it will now be able to fully animate it as I intended. (I think it looks really good.)

    I had plenty of questions to ask you, but I then read your comment "Don't ask why I don't know!" so I suspect that answers one of my questions! However, is it a general rule/recommendation that you only declare variables inside the Sub for object references?

    Otherwise, are there any good sources of information/books on coding VBA PowerPoint? I have not been able to find many. Information on topics like using the animation timeline and differences between using commands in show and normal modes would be appreciated.

    Also, is there a program which will convert the powerpoint program (with the VBA) into an online program i.e. HTML5 that you know of and recommend?

    Thank you again and for any comments!

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Declaring inside a sub means that the variable is only valid in that sub and that every time the sub runs it is set to Nothing / Null / zero. The general rule would be declare outsibe the sub if you want to use them in multiple subs. Typical example would be to have one sub that keeps a score and then l;ater a second sub that tells you the total schore.

    My guess is that oeff in your code needs to be set to Nothing to make it stable (this is probably either a bug or a timing issue)

    There really isn't a perfect vba book for PPT. Powerful PowerPoint for Educators is really the only one. There are lots of examples on their website from the book.

    Best way to learn in my experience is to ask people "How do I do this ..." or to study examples

    There are quite a few on our site in the vba section and also more on Steve Rindsberg's site in Programming Section

    http://www.pptalchemy.co.uk/powerpoi...rials.html#vba

    http://www.pptfaq.com
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    Hi - when you write set oeff to nothing do you do this at the start or at the end of every time you make an affect?

    Unfortunately, I have been busy adjusting my code, but I still get the same issue.

  6. #6
    I posted below after updating
    Last edited by ajarmstron; 07-09-2020 at 02:19 PM.

  7. #7
    Hi - I have gone through the relevant sub-procedures and declared all shape effects inside the sub-procedure. I have also Set the slides, shapes and effects as nothing.

    The good news is that the fill affect now works at all times! However, the bad news is that a new problem has arisen in that a shape which has used the described fill animation does not change its colour later on when using the command line MyDocument2.Shapes("AnswerRectangle" & TileAnswer(ActiveRectangle2)).Fill.ForeColor.RGB = RGB(255, 0, 0). I believe the command is processed as I have evidence that lines below this command are processed. Also when the slide show is exited the shape concerned has changed colour! So for some reason the outcome of the .Shape command is not being shown on the PowerPoint Slide Show.

    I would be grateful for any suggestions as to what I could try.
    Last edited by ajarmstron; 07-09-2020 at 02:44 PM.

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Sadly changes made in vba not updating in show view can be a problem. Are you using the latest version of Office? (It was more prevalent in earlier versions) Also mixing vba and animation can cause this.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    Yes - I am using Office 365.

    Otherwise, yes I am mixing VBA and animation. I have a concentration game where I have 16 cards that appear to turn over by the use of a collapse of the front shape and stretch of the back shape. After two cards are selected both cards change colour e.g. red or green. On the second selection I want the second card to turn over, the back of this card should be the colour change and the first selected card should also change colour ideally as the second card turns. My issue is that the first card appears to change colour before the second selected card turns over without using the animation method described in the original topic of this thread!

    I also don't seem to be able to use the collapse, stretch and then do the above fill colour change method on the two cards at the same time either!

  10. #10
    The other way to do this could be to make an animated gif consisting of a rectangle that changes colour (e.g. yellow to red). The question is, is there a program that can do this for me?)

    Edit - second thoughts this will not work as I am dealing with grouped shapes. I would still appreciate any ideas.
    Last edited by ajarmstron; 07-10-2020 at 06:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •