VBA PowerPoint:使用文本获取所有形状

时间:2013-04-11 23:00:49

标签: vba powerpoint-vba

无法弄清楚这里出了什么问题。 我得到一个没有为最后一个debug.print行设置的对象变量。 N.B - 循环中的debug.print行打印很好,并且有三个形状应该在数组中(并且我在循环结束时为3)。 我想我可能只是不明白数组/变量设置是如何工作的,我是VBA的新手(虽然我确实有编程经验)。

Dim allShapes As Shapes
Set allShapes = ActivePresentation.Slides(11).Shapes
Dim textShapes() As Shape
ReDim textShapes(0 To 2)
i = 0

For Each thisShape In allShapes
    If thisShape.HasTextFrame Then
        If thisShape.TextFrame.HasText Then
           Debug.Print thisShape.TextFrame.TextRange.Text
           Set textShapes(i) = thisShape
           i = i + 1

           ReDim textShapes(0 To i) As Shape
        End If

     End If
Next thisShape
ReDim textShapes(0 To i - 1)

Debug.Print textShapes(1).TextFrame.TextRange.Text

1 个答案:

答案 0 :(得分:4)

For Each thisShape In allShapes

什么是allShapes?是在某处宣布了吗?

另外,为了保留数组中的形状,您必须使用Redim Preserve

这是你在尝试什么?这将循环播放幻灯片1中的所有形状。

Sub Sample()
    Dim textShapes() As Shape, i as Long

    ReDim textShapes(0 To 2)

    i = 0

    For Each thisShape In ActivePresentation.Slides(1).Shapes
        If thisShape.HasTextFrame Then
            If thisShape.TextFrame.HasText Then
               Set textShapes(i) = thisShape
               i = i + 1
               ReDim Preserve textShapes(0 To i) As Shape
            End If
         End If
    Next thisShape

    Debug.Print textShapes(1).TextFrame.TextRange.Text
End Sub

同样问题的标题是Get all shapes with text;在这种情况下,您将不得不遍历数组。用文字获取所有形状。

相关问题