Get shape by Id or Name

两盒软妹~` 提交于 2019-11-27 08:04:21

问题


Is there any way to get a shape if you know its Id?

For example:

Dim myshape As Shape
myshape.Id = 42
myshape = getShapeById(myshape.Id)

Or, alternatively, could I get the shape by Name?

Dim myshape As Shape
myshape.Name = "Rectangle 42"
myshape = getShapeByName(myshape.Name)

回答1:


Getting a shape .Name by its .Id is somewhat more convoluted than getting its .Id by its .Name.

But here's how it's done:

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



回答2:


To get a Shape by Name, you do...:

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)



回答3:


Not sure about by ID, but by name use the sheet Shapes collection object

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

eg

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



回答4:


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

where k is the slide number and j and the shape number on that slide.

You can loop through each pages shapes with something like:

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

Chris



来源:https://stackoverflow.com/questions/5527073/get-shape-by-id-or-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!