This is wierd!

S

semiopen

Greetings

I am using Excel 2003 - I don't know how this would work in other
versions.

Open up a new workbook and put 2 shapes (I used ovals) on the sheet.
Then group them. In a code module add:

Sub disappointing()
Dim myName As String
Dim shp As Shape
myName = Sheets(1).Shapes("Group 3").GroupItems(1).name
MsgBox myName
Set shp = Sheets(1).Shapes("Group 3").GroupItems(myName)
MsgBox shp.name
End Sub

The first msgbox gives "Oval 1" as expected but the next line throws a
run-time "subscript out of range" error. GroupItems is expecting an
integer and can't handle a string like a full-fledged collection (such
as Shapes itself) could do - disappointing design if you ask me.

*But*

I you now enter:

Sub wierd()
Dim shp As Shape
For Each shp In Sheets(1).Shapes("Group 3").GroupItems
shp.Select
Selection.name = shp.name
Next shp
End Sub

And run it and *then* run disappointing () - you discover that the
subscript error has vanished and that both msgboxes work as hoped. What
is going on here? It seems that going through Selection is needed - it
somehow makes the shape names magic or something so that they cause
GroupItems, against all documentation, to start acting like a
collection.

-semiopen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top