Help with shape Id

  • Thread starter Thread starter Peter Jackson
  • Start date Start date
P

Peter Jackson

Hi All,
I having some problems getting a shape object base on the Id.
I can get:
ShpName = "Rectangle 1"
Set Shp = Sld.Shapes(ShpName)
But how do I do:
ShpId = 4561254
Set Shp = Sld.Shapes(ShpId)

TIA

Regards, Peter
 
You need to roll your own function. You can use the following
FindShapeById() function if it suits your requirement:

---
Function FindShapeById(ByVal Shps As Shapes, ByVal Id As Long) As Shape
Dim Shp As Shape

For Each Shp In Shps
If Shp.Id = Id Then
Set FindShapeById = Shp
Exit For
End If
Next
End Function
---

Use it as:
MsgBox FindShapeById(ActivePresentation.Slides(1).Shapes, 2050).Name

Note that the function may not find any shape with the specified Id. In that
case, it will return Nothing as the function result.

- Chirag

PowerShow - View multiple shows simultaneously
http://officerone.tripod.com/powershow/powershow.html
 
Hi Chirag,
Thank you very much, this works great.
I have converted your function to allow for groups, however, I have not
managed to find a simple why to determine if a shape exists without raising
an error.
Is there a simple way to determine that and object is Nothing?
The following works, but is there a better way:

Function FindShapeById(ByVal Shps As Shapes, ByVal Id As Long) As Shape
Dim Shp As Shape
Dim testShapeName As String
On Error Resume Next
For Each Shp In Shps
If Shp.Id = Id Then
Set FindShapeById = Shp
Exit For
End If
If Shp.Type = msoGroup Then
Set FindShapeById = FindGroupShapeById(Shp, Id)
testShapeName = FindShapeById.Name
If Err.Number = 0 Then
Exit For
End If
End If
Next
End Function

Function FindGroupShapeById(ByVal GrpShp As Shape, ByVal Id As Long) As
Shape
Dim Shp As Shape
Dim testShapeName As String
On Error Resume Next
gCnt = GrpShp.GroupItems.Count
For gNum = 1 To gCnt
Set Shp = GrpShp.GroupItems.Item(gNum)
If Shp.Id = Id Then
Set FindGroupShapeById = Shp
Exit For
End If
If Shp.Type = msoGroup Then
Set FindGroupShapeById = FindGroupShapeById(Shp, Id)
testShapeName = FindGroupShapeById.Name
If Err.Number = 0 Then
Exit For
End If
End If
Next
End Function

Thanks again for your help.

Regards, Peter
 
I have modified my FindShapeById() function to consider group shapes. Here
it is:

---
Function FindShapeById(ByVal Shps As Object, ByVal Id As Long) As Shape
Dim I As Long

For I = 1 To Shps.Count
If Shps(I).Id = Id Then
Set FindShapeById = Shps(I)
Exit For
End If

If Shps(I).Type = msoGroup Then
Set FindShapeById = FindShapeById(Shps(I).GroupItems, Id)
If Not (FindShapeById Is Nothing) Then
Exit For
End If
End If
Next
End Function
---

As before, use it as:
MsgBox FindShapeById(ActivePresentation.Slides(1).Shapes, 2050).Name

- Chirag

PowerShow - View multiple shows simultaneously
http://officerone.tripod.com/powershow/powershow.html
 
Back
Top