如何在PowerPoint 2007中使用VBA找出两个文本框或形状重叠?

时间:2012-01-25 13:44:01

标签: vba powerpoint

他们是否可以通过PowerPoint(2007)中的VBA找出两个文本框或形状是否重叠?

由于

2 个答案:

答案 0 :(得分:3)

这应该让你去。

仍然需要考虑边界情况:

两种形状只是接触计数是否重叠?

线条厚度怎么样?形状大小不包括线条粗细,但粗线可能会导致形状视觉重叠。

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

答案 1 :(得分:1)

除非对我所缺少的问题有一些深刻的意义,否则答案是肯定的。您是否不知道如何访问形状的尺寸,或者您知道如何使用尺寸来确定形状是否重叠?

以下宏将每张幻灯片上每个形状的尺寸输出到立即窗口。有了这些信息,检查重叠就不难了。

Option Explicit
Sub DsplDimensions()

  Dim InxSlide As Long
  Dim InxShape As Long

  With ActivePresentation
    For InxSlide = 1 To .Slides.Count
      Debug.Print "Slide " & InxSlide
      With .Slides(InxSlide)
        For InxShape = 1 To .Shapes.Count
          With .Shapes(InxShape)
            Debug.Print " Shape " & InxShape
            Debug.Print "  Top & left " & .Top & " " & .Left
            Debug.Print "  Height & width " & .Height & " " & .Width
          End With
        Next
      End With
    Next
  End With