Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PowerPoint Macro Bring Object To Front

785 views
Skip to first unread message

jim_epler

unread,
Jul 8, 2010, 1:58:43 PM7/8/10
to
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
----
Sub Bring4ToFront()
ActivePresentation.Slides(2).Shapes(4).ZOrder msoBringToFront
End Sub
----
Sub Bring5ToFront()
ActivePresentation.Slides(2).Shapes(5).ZOrder msoBringToFront
End Sub
----
Sub Bring6ToFront()
ActivePresentation.Slides(2).Shapes(6).ZOrder msoBringToFront
End Sub

Steve Rindsberg

unread,
Jul 9, 2010, 10:27:07 AM7/9/10
to
In article <ac9f8aa9-b1d1-4040-91b2-

72609e...@m40g2000prc.googlegroups.com>, Jim_epler wrote:
> 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.

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.

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

jim_epler

unread,
Jul 9, 2010, 2:45:58 PM7/9/10
to
Thanks for your reply. See below...

>On Jul 9, 7:27 am, Steve Rindsberg <st...@rdpslides.com> wrote:
> 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.

>On Jul 9, 7:27 am, Steve Rindsberg <st...@rdpslides.com> wrote:
> 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...

Steve Rindsberg

unread,
Jul 9, 2010, 11:19:52 PM7/9/10
to
[snipsnip]

> stack. I was under the impression that an object maintained its shape
> identifier regardless of object order on a given slide

There are a couple of ways of identifying shapes.

Each shape has a name that PPT assigns it when it's created (and that you
can change)

Each shape has an ID (a long that PPT assigns when it's created; it's
read-only, so you can't change it, but PPT won't change it either)

Then there's the ShapeIndex, which is what we use when we do something
like .Shapes(x)

The ShapeIndex is an index into the shapes *in their current z-order*.
When you change the z-order, the shape indices change. That's what bit
ya.

Or I am. I thought that you were going to call each of the subs in
order, all at once.

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:

jim_epler

unread,
Jul 12, 2010, 12:44:27 PM7/12/10
to
On Jul 9, 8:19 pm, Steve Rindsberg <st...@rdpslides.com> wrote:
> [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

Steve Rindsberg

unread,
Jul 13, 2010, 10:41:01 AM7/13/10
to
This all looks ok to me, Jim. Ah wait. No ...

This:


Sub BringToFront(lSlideIndex As Long, sShapeName As Shape)

should be:
Sub BringToFront(lSlideIndex As Long, sShapeName As String)

Give that a shot. If that fails to work (and it does work here in 2003), a couple of tests:

Select any of the shapes in question and run this to verify the name of the shape:

Sub Who_AM_I()
With ActiveWindow.Selection.ShapeRange(1)
MsgBox .Name
End With
End Sub

Also, try running any of the Sub TransparencyXToFront() routines in normal view to see if they work, or run
one in slide show view then quit the show to see if the shape order is orrect in normal view.

See, there are refresh bugs in PPT 2007; it fails to refresh the view properly in slide show view.

Sometimes it's sufficient to issue a

ActiveWindow.View.GoToSlide(2) ' or whatever slide index you're on already

Other times, sterner measures are called for.

jim_epler

unread,
Jul 13, 2010, 1:21:30 PM7/13/10
to
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

Steve Rindsberg

unread,
Jul 13, 2010, 5:13:53 PM7/13/10
to
Ah, great. It's always some silly little thing that bites us.
Just that FINDING it turns into a silly big thing. ;-)

By the way, microsoft has shut down their end of this newsgroup. That's why
there's so little traffic on it in the last month or so.

I'm sure a couple of us will continue to check it fairly often, but you might
want to check out the new Answers web forum.

The PowerPoint Support Forum, Newsgroup, etc.
http://www.pptfaq.com/FAQ00275.htm

Not all that welcoming to a dedicated "newsie", but it's where MS wants us to
go today.


In article <d2ae806e-00a8-4a02-b279-

0 new messages