按ID或名称获取形状

时间:2011-04-03 01:55:32

标签: vba powerpoint powerpoint-vba

如果您知道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)

4 个答案:

答案 0 :(得分:5)

通过.Name获取形状.Idgetting its .Id by its .Name更复杂。

但是这是如何完成的:

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

答案 1 :(得分:3)

要按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)

答案 2 :(得分:0)

不确定ID,但按名称使用工作表Shapes集合对象

Set myShape = <SheetObject>.Shapes("<ShapeName>")

例如

Set myShape = ActiveSheet.Shapes("Rectangle 1")

答案 3 :(得分:0)

sName = ActivePresentation.Slides(k).Shapes(j).Name

其中k是幻灯片编号和j以及幻灯片上的形状编号。

您可以使用以下内容遍历每个页面形状:

k = 1
For j = 1 To ActivePresentation.Slides(k).Shapes.Count
Next j

克里斯