【问题标题】:VBA PowerPoint: Get all shapes with textVBA PowerPoint:使用文本获取所有形状
【发布时间】:2015-07-28 20:10:33
【问题描述】:

无法完全弄清楚这里出了什么问题。 我得到一个未为最后一个 debug.print 行设置的对象变量。 注意 - 循环中的 debug.print 行打印良好,并且数组中应该有三个形状(并且 i 在循环结束时位于 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

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

    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;在这种情况下,您将不得不遍历数组。获取所有带有文本的形状。

    【讨论】:

    • 是的,保护区做到了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2017-10-21
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多