通过ID或名称获取形状

如果你知道它的
Id
,有什么方法可以得到一个形状吗? 例如:
Dim myshape As Shape
myshape.Id = 42
myshape = getShapeById(myshape.Id)
或者,或者,我可以通过
Name
获得形状吗?
Dim myshape As Shape
myshape.Name = "Rectangle 42"
myshape = getShapeByName(myshape.Name)
    
已邀请:
通过它的
.Id
获得一个形状
.Name
比通过它的
.Name
获得它的
.Id
更复杂。 但这是如何完成的:
Sub PrintShapeName()
    Debug.Print getNameByID(3, 1)
End Sub

Function getNameByID(shapeID As Long, slide As Integer)
    Dim ap As Presentation: Set ap = ActivePresentation
    Dim sl As slide: Set sl = ap.Slides(slide)
    sl.Shapes.SelectAll
    Dim sr As ShapeRange
    Set sr = Windows(1).Selection.ShapeRange
    Dim s As Shape
    For Each s In sr
        If s.id = shapeID Then
            getNameByID = s.Name
            Exit Function
        End If
    Next
End Function
    
要获得
Shape
Name
,你会......
Function getShapeByName(shapeName As String, Slide As Integer)
    Set getShapeByName = ActivePresentation.Slides(Slide).Shapes(shapeName)
End Function

Dim myshape As Shape
myshape = getShapeByName("Rectangle 42", 1)
    
不确定ID,但通过名称使用工作表Shapes集合对象
Set myShape = <SheetObject>.Shapes("<ShapeName>")
例如
Set myShape = ActiveSheet.Shapes("Rectangle 1")
    
sName = ActivePresentation.Slides(k).Shapes(j).Name
其中
k
是幻灯片编号和
j
以及幻灯片上的形状编号。 您可以使用以下内容遍历每个页面形状:
k = 1
For j = 1 To ActivePresentation.Slides(k).Shapes.Count
Next j
克里斯     

要回复问题请先登录注册