通过 ID 访问 Shape 对象
Access a Shape object via its ID
我有一张 PowerPoint 幻灯片,上面有 5 个形状。我想用宏中的这些形状做不同的事情。如何使用形状 ID 更改这些形状之一?例如,我有两个名称为 "Title 1"
的形状,但我想使用 ID 为 15 的形状。
这是我的代码:
Sub size_n_spread_v()
Dim j As Integer
Dim sld As Slide
Dim SldId As Long
gap = std_gap
SldId = ActiveWindow.View.Slide.SlideIndex
Set sld = ActivePresentation.Slides(SldId)
Call SortMultArray
new_dim = (total_dim - gap * (lngRow - 1)) / lngRow
'This works but is not specific:
'sld.Shapes.("Title 1").Height = new_dim
'This would hopefully be specific but the syntax does not work Please HELP!
'sld.Shapes.("Title 1").Id(15).Height = new_dim
End Sub
有人知道通过 ID 更改形状的正确语法吗?
我不知道有什么办法,但您可以编写一个简单的辅助函数,然后您可以在整个项目中使用它来让事情变得更轻松。像这样的东西会起作用:
Public Function GetShapeById(s As Slide, n As String, id As Long) As Shape
Dim objShape As Shape
For Each objShape In s.Shapes
If StrComp(objShape.Name, n, vbTextCompare) = 0 And objShape.Id = id Then
Set GetShapeById = objShape
Exit Function
End If
Next
End Function
然后你就可以这样使用了:
Sub size_n_spread_v()
....
' Instead of:
sld.Shapes.("Title 1").Id(15).Height = new_dim
' Use:
GetShapeById(sld, "Title 1", 15).Height = new_dim
End Sub
上面提到的函数是唯一通过Id获取形状的方法。您必须搜索 Shapes 集合,因为没有与 SlideIndex 等效的 ShapeIndex。另一种查找特定形状的解决方案是通过添加您自己的标签来唯一标识形状,但这是一个更复杂的解决方案。
我有一张 PowerPoint 幻灯片,上面有 5 个形状。我想用宏中的这些形状做不同的事情。如何使用形状 ID 更改这些形状之一?例如,我有两个名称为 "Title 1"
的形状,但我想使用 ID 为 15 的形状。
这是我的代码:
Sub size_n_spread_v()
Dim j As Integer
Dim sld As Slide
Dim SldId As Long
gap = std_gap
SldId = ActiveWindow.View.Slide.SlideIndex
Set sld = ActivePresentation.Slides(SldId)
Call SortMultArray
new_dim = (total_dim - gap * (lngRow - 1)) / lngRow
'This works but is not specific:
'sld.Shapes.("Title 1").Height = new_dim
'This would hopefully be specific but the syntax does not work Please HELP!
'sld.Shapes.("Title 1").Id(15).Height = new_dim
End Sub
有人知道通过 ID 更改形状的正确语法吗?
我不知道有什么办法,但您可以编写一个简单的辅助函数,然后您可以在整个项目中使用它来让事情变得更轻松。像这样的东西会起作用:
Public Function GetShapeById(s As Slide, n As String, id As Long) As Shape
Dim objShape As Shape
For Each objShape In s.Shapes
If StrComp(objShape.Name, n, vbTextCompare) = 0 And objShape.Id = id Then
Set GetShapeById = objShape
Exit Function
End If
Next
End Function
然后你就可以这样使用了:
Sub size_n_spread_v()
....
' Instead of:
sld.Shapes.("Title 1").Id(15).Height = new_dim
' Use:
GetShapeById(sld, "Title 1", 15).Height = new_dim
End Sub
上面提到的函数是唯一通过Id获取形状的方法。您必须搜索 Shapes 集合,因为没有与 SlideIndex 等效的 ShapeIndex。另一种查找特定形状的解决方案是通过添加您自己的标签来唯一标识形状,但这是一个更复杂的解决方案。