Consulting

Results 1 to 5 of 5

Thread: Align middles of two shapes with each other, but NOT with the slide?

  1. #1
    VBAX Regular
    Joined
    Mar 2019
    Posts
    73
    Location

    Align middles of two shapes with each other, but NOT with the slide?

    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

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Regular
    Joined
    Mar 2019
    Posts
    73
    Location
    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)

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    Last edited by John Wilson; 09-19-2019 at 10:50 AM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Regular
    Joined
    Mar 2019
    Posts
    73
    Location
    Mr. Wilson, you are one smart dude!! That worked perfectly! Thank you very much for seeing this through

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •