PowerPoint Macro Bring Object To Front

  • Thread starter Thread starter jim_epler
  • Start date Start date
J

jim_epler

Hello,

I'm attempting to order six picture objects (on slide 2) based on the
click of an action button (PP 2003). I've created the following macro
routine that sometimes works, but it's not consistent. Any help would
be appreciated.

----
Sub Bring1ToFront()
ActivePresentation.Slides(2).Shapes(1).ZOrder msoBringToFront
End Sub
----
Sub Bring2ToFront()
ActivePresentation.Slides(2).Shapes(2).ZOrder msoBringToFront
End Sub
----
Sub Bring3ToFront()
ActivePresentation.Slides(2).Shapes(3).ZOrder msoBringToFront
End Sub
 
Thanks for your reply. See below...
You don't describe what sort of inconsistencies you're seeing but I'm
guessing that the final order of pictures varies depending on the z-
order when you start.  Actually that's expected.

To clarify, when the applicable button is first clicked the correct
picture (or shape) is initially brought to the front, but if that
button is clicked again a different picture will appear on top of the
stack. I was under the impression that an object maintained its shape
identifier regardless of object order on a given slide, so your
explanation below makes sense as to why it isn't working the way I'd
hoped.
When you ask for .Shapes(1) you get the shape that's first (ie behind
everything in Z-order).  When you move it to the front, it's now
.Shapes(.Shapes.Count).  The index by which you're addressing the
shapes IS the Z-order, so when you change the Zorder, you're changing
the numbers of the shapes.

You're probably better off working with shape names:

' On one line:
ActivePresentation.Slides(2).Shapes("Rectangle 2").ZOrder
msoBringToFront
' substitute the name of each shape you want to work with

Or to save typing:

Sub DoYourStuff()
   With ActivePresentation
     BringToFront 2, "Rectangle 2"
     BringToFront 2, "Rectangle 5"
     ' and so on
   End With
End Sub

Sub BringToFront(lSlideIndex as Long, sShapeName as Shape)

        ' this should be one one line!
  ActivePresentation.Slides(lSlideIndex).Shapes(sShapeName).ZOrder
msoBringToFront

End Sub

I'm not having success with the "save typing" example shown above. I'm
assuming I should paste both sub routines into the Module1 code
window, substituting only the text "Rectangle 1", etc. with the name
of my corresponding objects "Transparency 1", etc. in the first sub
routine. I should then apply the macro named "DoYourStuff" as an
action setting to each button the page. No? Maybe I'm missing
something...
 
[snipped]

But instead, I think you'd want Button 1, which brings the shape named
Bubba to the front to call code like this:

Sub BubbaFrontAndCenter()
' Bring shape named Bubba on slide 2 to front
BringToFront 2, "Bubba"
End Sub

And you can easily modify that for the other buttons.
Each of them calls this to do the actual lifting:

Sub BringToFront(lSlideIndex as Long, sShapeName as Shape)
' this should be one one line!
ActivePresentation.Slides(lSlideIndex).Shapes(sShapeName).ZOrder
msoBringToFront
End Sub

hmmm... still not working. I included my code below. I'm 98% sure the
shape names are correct. I double-checked using the custom animation
window. Anything look amiss?


Sub BringToFront(lSlideIndex As Long, sShapeName As Shape)
' Author Steve Rindsberg
' ref http://groups.google.com/group/microsoft.public.powerpoint/browse_frm/thread/8044b8554486c9b5?hl=en
ActivePresentation.Slides(lSlideIndex).Shapes(sShapeName).ZOrder
msoBringToFront
End Sub
Sub Transparency1ToFront()
' Bring shape named Transparency1 on slide 2 to front
BringToFront 2, "Transparency 1"
End Sub
Sub Transparency2ToFront()
' Bring shape named Transparency2 on slide 2 to front
BringToFront 2, "Transparency 2"
End Sub
Sub Transparency3ToFront()
' Bring shape named Transparency3 on slide 2 to front
BringToFront 2, "Transparency 3"
End Sub
Sub Transparency4ToFront()
' Bring shape named Transparency4 on slide 2 to front
BringToFront 2, "Transparency 4"
End Sub
Sub Transparency5ToFront()
' Bring shape named Transparency5 on slide 2 to front
BringToFront 2, "Transparency 5"
End Sub
Sub Transparency6ToFront()
' Bring shape named Transparency6 on slide 2 to front
BringToFront 2, "Transparency 6"
End Sub
 
OKAY... now we're cooking!! That tweak and the code you suggested to
determine the correct shape name did the trick! I discovered my shapes
are NOT named "Transparency 1," etc. but are actually referred to as
"Picture 5," "Picture 14," etc. I guess my method of determining an
object's shape name using the custom animation pane isn't the proper
way to do it after all!

Thanks so much for your help, Steve. I'm sure the teacher who will be
using this PowerPoint in his classroom (with digital inking on a
tablet) will be quite happy when he sees how this will work! :)

-Jim
 
Back
Top