按ID或名称获取形状

Mat*_*haq 5 powerpoint vba powerpoint-vba

如果你知道它有没有办法获得一个形状Id

例如:

Dim myshape As Shape
myshape.Id = 42
myshape = getShapeById(myshape.Id)
Run Code Online (Sandbox Code Playgroud)

或者,或者,我可以通过Name

Dim myshape As Shape
myshape.Name = "Rectangle 42"
myshape = getShapeByName(myshape.Name)
Run Code Online (Sandbox Code Playgroud)

Tod*_*ain 5

.Name通过它获得一个形状.Id通过它获得它.Id.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
Run Code Online (Sandbox Code Playgroud)


Mat*_*haq 4

为了度过难关,ShapeName需要......:

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)
Run Code Online (Sandbox Code Playgroud)