Get shape by Id or Name

前端 未结 4 1487
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 18:55

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)         


        
相关标签:
4条回答
  • 2020-12-11 19:01

    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)
    
    0 讨论(0)
  • 2020-12-11 19:04

    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
    
    0 讨论(0)
  • 2020-12-11 19:11
    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

    0 讨论(0)
  • 2020-12-11 19:18

    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")
    
    0 讨论(0)
提交回复
热议问题