Select the first shape in a selection

E

Edward Thrashcort

I just can't work out a method to select the first shape contained within
the current Selection.

Shapes do not appear to be extractable from the Selection object and each
shape on the Document object is unwilling to tell me its Range so I can test
its position relative to the current Selection.

I'm sure this must be an FAQ?

Eddie
 
G

Greg Maxey

Edward,

Will this do:
Sub ScratchMacro()
Selection.Range.ShapeRange(1).Select
End Sub
 
E

Edward Thrashcort

Thanks for that idea. Unfortunately this does not work if the shape is an
inlineshape. Clearly I don't understand the object model for graphics, but
I want to select the first picture in a range whether it's floating or
inline with the text.

If it's floating, does it have a range start and a range end relative to the
text that its included-with?

Eddie
 
G

Greg Maxey

Eddie,

Maybe this will work:

Sub ScratchMacro()
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Dim x As Long
Dim y As Long
Set oRng1 = Selection.Range
Set oRng2 = Selection.Range
oRng1.ShapeRange(1).Select
x = Selection.Range.Start
oRng2.InlineShapes(1).Select
y = Selection.Range.Start
If x < y Then
oRng1.ShapeRange(1).Select
Else
oRng2.InlineShapes(1).Select
End If
End Sub
 
E

Edward Thrashcort

Cheers Greg, that does work OK. I'll have to Count that there are ANY of
each type in the selection before trying to select them - otherwise an error
occurs - but that will work OK.

Your solution is obvious when I see it - I was assuming that InlineShapes
and ShapeRanges would be children of some parent "all graphics" object but
apparently not.

Eddie
 
G

Greg Maxey

Eddie,

Yes your right. I tested on a simple document containing exactly 2
shapes, one inline and one in the drawing layer, and switched them
around.

There may be a more direct approach to this. I will be the first to
admit that I am not very familiar with working with graphics and
shapes.

Glad I could help.
 
T

Tony Jollans

A small word of warning here: you need to be clear about what you want to
select. Shapes (not InlineShapes) will be included in the Selection if their
*anchor* is within the selection, wherever they happen to be actually
positioned.
 
E

Edward Thrashcort

Yes Tony I've just discovered that a Shape's Range is located at its anchor
point and although it may appear to be included in the current selection, if
it's anchor point is outside, then its not!

If the only thing that is in the selection is a shape, then I've decided to
put out a message recommending that it be converted to inline.

Eddie
 

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