PDA

View Full Version : [SOLVED:] Align middles of two shapes with each other, but NOT with the slide?



ajjava
09-19-2019, 06:53 AM
I've written enough code to select two shapes by name on a slide, and align their middles. BUT, the code is also aligning those two shapes with the middle of the SLIDE and I don't want that. To refine further, I'd like to be certain that the "Picture 2" will align to the middle of the "Rectangle 2" (in other words, the rectangle's position is static and should not change...I just want to be sure that the picture middle-aligns with the rectangle). Can anyone assist?


If oSh.Name = "Rectangle 2" And oSh.Height > 100 Then
oSh.Select
oSl.Shapes.Range(Array("Picture 2")).Select msoFalse 'This is how you select a particular shape by its name. The msoFalse means to ADD the shape to the already selected shape
oSl.Shapes.Range.Align msoAlignMiddles, True

Else
If oSh.Name = "Rectangle 2" Then
oSh.Select
oSl.Shapes.Range(Array("Picture 2")).Select msoFalse
End If
End If

John Wilson
09-19-2019, 08:02 AM
By setting the Align to True you are telling it to align to slide.
Also you do not need to use an Array if you are selecting just one thing


osl.Shapes("Picture 2").Select msoFalse 'This is how you select a particular shape by its name. The msoFalse means to ADD the shape to the already selected shape
osl.Shapes.Range.Align msoAlignMiddles, False

ajjava
09-19-2019, 08:19 AM
Thanks, John. I tried using "False" and both of the shapes still shifted on the page, so that they align with the middle of the slide.
(P.S. I'm using an array because there will be more picture names to add)

John Wilson
09-19-2019, 10:38 AM
Both shapes will shift but not (necessarily) align to the centre of the slide. The Microsoft alignment tools are fairly stupid and use a sort of average if you choose Middle or Centre!

The answer is to rewrite the Align tools


Sub aligner()
Dim osl As Slide
Dim osh As Shape
Dim osh1 As Shape
Dim osh2 As Shape
Set osl = ActiveWindow.Selection.SlideRange(1)
For Each osh In osl.Shapes
If osh.Name = "Rectangle 2" And osh.Height > 100 Then
Set osh1 = osh
End If
If osh.Name = "Picture 2" Then
Set osh2 = osh
End If
Next osh
If Not osh1 Is Nothing And Not osh2 Is Nothing Then
osh2.Top = osh1.Top + osh1.Height / 2 - osh2.Height / 2
End If
End Sub

ajjava
09-19-2019, 01:08 PM
Mr. Wilson, you are one smart dude!! That worked perfectly! Thank you very much for seeing this through :)