Is there any way to get a shape's Id if you know it's Name?

For example, if I have this:

Dim myshape As Shape
myshape.Name

Can I get it's Id?

myshape.Id = getIdByName(myshape.Name)
有帮助吗?

解决方案

Sure, it's pretty straigtforward:

Sub PrintShapeID()
    Debug.Print getIDByName("My Shape", 1)
End Sub

Function getIDByName(shapeName As String, slide As Integer)
    Dim ap As Presentation: Set ap = ActivePresentation
    Dim sl As slide: Set sl = ap.Slides(slide)
    Dim sh As Shape: Set sh = sl.Shapes(shapeName)
    getIDByName = sh.Id
End Function

This works for the slide that you specify. You can also loop through all slides, but note that there may be more than one shape with the same name so you'd have to figure out which one you want.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top