VBA PowerPoint:获取所有带有文本的形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15960333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA PowerPoint: Get all shapes with text
提问by JoshDG
Can't quite figure out what's going wrong here. I get a object variable not set for the last debug.print line. N.B - the debug.print line in the loop prints fine and there are three shaped that should be in the array (and i is at 3 at the end of the loop). I think I may just not understand exactly how arrays / variable setting works, I'm new to VBA (I do have programming experience though).
无法完全弄清楚这里出了什么问题。我得到一个未为最后一行 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
回答by Siddharth Rout
For Each thisShape In allShapes
For Each thisShape In allShapes
What is allShapes
? Is it declared somewhere?
什么是allShapes
?它是在某处声明的吗?
Also to preserve the shapes in the array you have to use Redim Preserve
还要保留您必须使用的数组中的形状 Redim Preserve
Is this what you are trying? This loops thorough all the shapes in Slide 1.
这是你正在尝试的吗?这将遍历幻灯片 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
Also as the title of the question say Get all shapes with text
; In such a case you will have to loop through the array. to get all shapes with text.
也正如问题的标题所说Get all shapes with text
;在这种情况下,您将不得不遍历数组。获取带有文本的所有形状。