Search code examples
vbapowerpoint

How To Find Out If Two Textboxes Or Shapes Overlap Using VBA in PowerPoint 2007?


Is their any way to find out if two textboxes or shapes overlap using VBA in PowerPoint (2007)?

Thanks


Solution

  • This should get you going.

    It still needs to account for boundary cases:

    Do two shapes just touching count as an overlap or not?

    What about line thickness? The shape size doesn't include the line thickness, but a thick line might cause the shapes to visual overlap.

    Function ShapesOverlap(oSh1 As Shape, oSh2 As Shape) As Boolean
        Dim Shp1Left As Single
        Dim Shp1Right As Single
        Dim Shp1Top As Single
        Dim Shp1Bottom As Single
    
        Dim Shp2Left As Single
        Dim Shp2Right As Single
        Dim Shp2Top As Single
        Dim Shp2Bottom As Single
    
        Dim bHorizontalOverlap As Boolean
        Dim bVerticalOverlap As Boolean
    
        With oSh1
            Shp1Left = .Left
            Shp1Right = .Left + .Width
            Shp1Top = .Top
            Shp1Bottom = .Top + .Height
        End With
    
        With oSh2
            Shp2Left = .Left
            Shp2Right = .Left + .Width
            Shp2Top = .Top
            Shp2Bottom = .Top + .Height
        End With
    
        ' do they overlap horizontally?
        If Shp1Left > Shp2Left Then
            If Shp1Left < Shp2Right Then
                bHorizontalOverlap = True
            End If
        End If
        If Shp1Left < Shp2Left Then
            If Shp1Right > Shp2Left Then
                bHorizontalOverlap = True
            End If
        End If
    
        ' do they overlap vertically?
        If Shp1Top > Shp2Top Then
            If Shp1Top < Shp2Bottom Then
                bVerticalOverlap = True
            End If
        End If
        ' do they overlap vertically?
        If Shp1Top < Shp2Top Then
            If Shp1Bottom > Shp2Top Then
                bVerticalOverlap = True
            End If
        End If
    
        ShapesOverlap = bHorizontalOverlap And bVerticalOverlap
    
    End Function