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)
Todd Main
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
Mateen Ulhaq
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)
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")
Chris Christiansson
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