'Shapes.Count' ≠ 形状索引#!
The .Count
与当前形状的上限不同.Index
数字。
通过列出文档中的所有形状,可以更容易地理解编号系统:
Sub ListShapes()
'hit CTRL+G to view output in Immediate Window
Dim sh As Shape, sld As Slide, idx As Long
Set sld = ActivePresentation.Slides(1) '<-- change to your slide number
For Each sh In sld.Shapes
idx = idx + 1
Debug.Print "Shape ID#" & sh.Id, "Index #" & idx, "Name: " & sh.Name
Next sh
Debug.Print "Count of shapes: " & sld.Shapes.Count
End Sub
NOTE: There is alternative code for Excel at the bottom of this post!
为了证明,我们可以添加形状到一个新文档:
请注意,Index不是此对象的属性,但它按照 Excel 在内存中存储形状的顺序进行计数(与返回的顺序相同)For Each..Next
陈述。
了解 Excel 如何存储形状的另一种方法是使用观察窗。添加断裂线或Stop
在循环中间,然后突出显示ws.Shapes
,右键单击它,选择Add Watch...
并单击“确定”。浏览树以发现文档中形状的不同属性/属性。
-
接下来,如果我们delete再次运行上面的过程,我们将得到:
Shape ID#2 Index #1 Name: Rectangle 1
Shape ID#3 Index #2 Name: Rectangle 2
Shape ID#5 Index #3 Name: Rectangle 4
Shape ID#6 Index #4 Name: Rectangle 5
Count of shapes: 4
The ID
and Name
其余形状不会改变,但是the Index被重新编号以体现新的“秩序”。
...从而返回名称Rectangle 5
我们现在需要使用:
Debug.Print ActivePresentation.Slides(1).Shapes(4).Name
参考形状(包括controls)
当您通过数字引用形状时,例如.Shapes(????)
,你指的是形状索引号????,不是ID
数字。索引号根据需要动态分配,因此不是一个稳定的方法来指代一种形状。
理想情况下,您应该通过.Name
or .ID
数字。如果动态生成形状,您最好将形状列表存储在数组或集合中,以便您可以根据需要查看该列表。
检索“最后创建的形状”
如果使用的唯一原因是索引号是检索“最后创建的形状”,那么您可以使用这样的函数来获取索引号:
Function idxLastShape(slideNum As Long) As Long
Dim sh As Shape
For Each sh In ActivePresentation.Slides(slideNum).Shapes
idxLastShape = idxLastShape + 1
Next sh
End Function
用法示例:
Debug.Print idxLastShape(1) 'Returns index of last shape on slide#1
NOTE: There is alternate code for Excel at the bottom of this post!
或者,您可以让该函数返回对实际的引用形状物体,而不是数字,如下所示:
Function LastShape(slideNum As Long) As Shape
Dim sh As Shape
For Each sh In ActivePresentation.Slides(slideNum).Shapes
Set LastShape = sh
Next sh
End Function
...所以你可以通过以下方式获得“最后一个形状”的名称:
Debug.Print LastShape(1).Name
删除最近创建的形状
使用上面的函数,您可以使用通常用于形状的任何方法。例如,您可以删除在幻灯片 #1 上创建的“最后一个形状”:
LastShape(1).Delete
CAUTION!
帖子中的例子(包括删除示例!)不分青红皂白地返回/编辑/删除什么类型的形状!
有dozens形状类型,从图形到声音/视频以及controls。您可以使用以下方法过滤这些过程所枚举的形状.Type
的财产Shape
对象,以及其他方法。有一个部分列表here,以及以下链接中的更多信息。
Excel 的替代代码:
列出工作表上的所有形状 (Excel)
Sub ListShapes()
'hit CTRL+G to view output in Immediate Window
Dim sh As Shape, ws As Worksheet, idx As Long
Set ws = Sheets("Sheet1") '<-- change to your worksheet name
For Each sh In ws.Shapes
idx = idx + 1
Debug.Print "Shape ID#" & sh.ID, "Index #" & idx, "Name: " & sh.Name
Next sh
Debug.Print "Count of shapes: " & Sheets("Sheet1").Shapes.Count
End Sub
返回“最后形状”的索引号(Excel)
Function idxLastShape(shtName As String) As Long
Dim sh As Shape
For Each sh In Sheets(shtName).Shapes
idxLastShape = idxLastShape + 1
Next sh
End Function
用法示例: Debug.Print idxLastShape("Sheet1")
返回对“最后一个形状”对象的引用 (Excel)
Function LastShape(shtName As String) As Shape
Dim sh As Shape
For Each sh In Sheets(shtName).Shapes
Set LastShape = sh
Next sh
End Function
用法示例: Debug.Print LastShape("Sheet1").Name
更多信息:
- MSDN : Shapes Object(PowerPoint/VBA)
- MSDN : Shapes Object(Excel/VBA)
- MSDN : MsoShape类型枚举(办公室)
- 堆栈溢出 :合作概述表单控件 and ActiveX 控件
- MSDN : 使用形状(绘制对象)
- 办公室.com:如何添加形状
- 微风树:使用 VBA 编程形状(自选图形)
- 聪明的猫头鹰:使用形状(教程)
从 Excel 复制到 Powerpoint 的其他方法:
- 电子表格大师:使用 VBA 将 Excel 范围复制并粘贴到 PowerPoint 中
- ExcelOffTheGrid:使用 VBA 从 Excel 控制 Powerpoint
- mvps.org:将 Excel 图表粘贴为 PowerPoint 中的图片(特殊粘贴)