PDA

View Full Version : positioned second from front in powerpoint vba



Jhon90
06-26-2021, 12:05 PM
Sir,

I have 40 different shapes & one group(say group1). In selection pane position of the group1 is 12th from the top. Now I want to position group1 second from top.
Is it possible with vba code?
I found ZOrderPosition property & Zorder msoBringForward but not configure out. please help.

John Wilson
06-26-2021, 01:10 PM
There is no command to move to a specific zorderposition

In your particular case you could do it in 2 steps


Sub moveIT()
Dim osld As Slide
'change to actual slide number
Set osld = ActivePresentation.Slides(1)
osld.Shapes("Group1").ZOrder (msoBringToFront)
osld.Shapes("Group1").ZOrder (msoSendBackward)
End Sub

More generally you would have to use a loop


Sub moveIT2()
Dim osld As Slide
Set osld = ActivePresentation.Slides(1)
Do
osld.Shapes("Group1").ZOrder (msoBringForward)
Loop While osld.Shapes("Group1").ZOrderPosition < osld.Shapes.Count - 1
End Sub

Jhon90
06-27-2021, 01:29 PM
Sir thanks for helping me.
first code works perfectly.
But when I run the second code(loop) it goes only one step forward. I attach a presentation where I want to position Group 17 in second from top but it goes only one step forward. Please see presentation example11.pptm

John Wilson
06-28-2021, 03:55 AM
I see that too. Groups can do strange things when you play with the zordrposition. use the code that works!

Jhon90
06-28-2021, 04:30 AM
Thanks Sir

I found a code in https://docs.microsoft.com/ to places the shape second from the back & its working with group also.
But I need exactly opposite to that.


Sub move()
Dim osld As Slide
'change to actual slide number
Set osld = ActivePresentation.Slides(1)
With osld.Shapes("Group 17")
While .ZOrderPosition > 2
.ZOrder msoSendBackward
Wend


End With

End Sub

I manipulate(msoSendForward) with this code but not get desire result.

John Wilson
06-28-2021, 07:38 AM
It is pretty difficult to do especially with Groups.

MSFT handle group0s in a verey strange way. A group with say three mshapes inside is treated as FOUR shapes in some places but only 1 in others.

also zorderposition 1 is the back but you have to calculate what the front would be - bearing in mind how group are counted this is not simple

This should work but make sure you work on a copy as it might crash.


Sub moveIT2()
Dim osld As Slide
Dim TopZ As Long
Dim GI As Long
Set osld = ActivePresentation.Slides(1)
GI = osld.Shapes("Group 17").GroupItems.Count
' add a shape and check the zorder for the new front then delete it
With osld.Shapes.AddShape(msoShapeRectangle, 1, 1, 1, 1)
TopZ = .ZOrderPosition - 1
.Delete
End With
Do
osld.Shapes("Group 17").ZOrder (msoBringForward)
' check the last groupitem not the group itself
Loop While osld.Shapes("Group 17").GroupItems(GI).ZOrderPosition < TopZ - 1
End Sub

Jhon90
06-29-2021, 06:44 AM
So many Thanks Sir,

Its perfectly working now. I have no words to appreciate you. You gave valuable suggestion whenever I struck. May god bless you.