Bringing certain shapes on top of others in your slides
Problem
A user on the PowerPoint newsgroup needed a way of making sure that certain shapes in his presentation were always on top of the other shapes on each slide, and that they were always "stacked" in a certain order.
In this case, all arrows were to be on top, all text boxes below the arrows, all pictures below the text boxes and everything else on the slide below that.
At first glance this seems easy: step through all of the shapes on each slide once for each type of shape, and if the shape is the type we're searching for, bring it to the top.
Unfortunately, this doesn't work, because the order of the shape collection gets changed each time you change the "stacking order".
Instead, we get a reference to each shape that meets our criteria, put it in an array, then once we've checked all the shapes, process the array, moving each shape to the top in order.
Solution
Sub StackEmDanO()
Dim oShapes As Shapes
Dim oSh As Shape
Dim oSl As Slide
Dim x As Long
Dim aShapes() As Shape
For Each oSl In ActivePresentation.Slides
' Redimension the array to hold the maximum number of shapes for this slide
ReDim aShapes(1 To oSl.Shapes.Count) As Shape
x = 1
' Add the shapes we want to affect to the array of shapes
' in the order we want them stacked
' Pictures
For Each oSh In oSl.Shapes
If oSh.Type = msoPicture Then
Set aShapes(x) = oSh
x = x + 1
End If
Next ' shape
' Textboxes
For Each oSh In oSl.Shapes
If oSh.Type = msoTextBox Then
Set aShapes(x) = oSh
x = x + 1
End If
Next ' shape
' Lines that have an arrowhead
For Each oSh In oSl.Shapes
If oSh.Type = msoLine Then
If oSh.Line.EndArrowheadStyle <> msoArrowheadNone _
Or _
oSh.Line.BeginArrowheadStyle <> msoArrowheadNone Then
Set aShapes(x) = oSh
x = x + 1
End If
End If
Next ' shape
' Now bring each shape in the array to the top
' in order
For x = 1 To UBound(aShapes)
If Not aShapes(x) Is Nothing Then
aShapes(x).ZOrder (msoBringToFront)
Debug.Print aShapes(x).Name
End If
Next
Next ' slide
End Sub
See How do I use VBA code in PowerPoint? to learn how to use this example code.
Search terms:order,shape,stack,z