Detect shapes anchored in paragraph

J

James

Hi

Is there any way to detect whether a paragraph (or range) contains an object
anchor?

Thanks
 
J

James

I don't think you can get the shapes collection from a range object. You can
go

Range.ShapeRange.Parent.Shapes

....but that just gets you back to the collection of shapes in the document
and no way to pin the current shape to the current range (that I can see)
 
J

Jezebel

Why not try it?

Each Range object (including Paragraph.Range) has a Shapes collection,
containing all the Shapes in that range. For a paragraph, use
Paragraph.Range.Shapes.
 
J

James

There's no Shapes collection from Range (you can get to InlineShapes but not
Shapes). Shapes is accessed via the Document or Header/Footer objects only I
think.

From the Range object, you get a ShapeRange collection, but it doesn't
contain any shapes! (I think ShapeRange only works properly from the
Selection object). I've tried testing for an error on Range.ShapeRange.Anchor
but the same error is generated if there are no shapes or if there are
multiple shapes, so it's not reliable for what I want to do.
 
T

Tony Jollans

Try Range.ShapeRange.Count

--
Enjoy,
Tony

James said:
There's no Shapes collection from Range (you can get to InlineShapes but not
Shapes). Shapes is accessed via the Document or Header/Footer objects only I
think.

From the Range object, you get a ShapeRange collection, but it doesn't
contain any shapes! (I think ShapeRange only works properly from the
Selection object). I've tried testing for an error on Range.ShapeRange.Anchor
but the same error is generated if there are no shapes or if there are
multiple shapes, so it's not reliable for what I want to do.
 
J

Jean-Guy Marcil

James was telling us:
James nous racontait que :
From the Range object, you get a ShapeRange collection, but it doesn't
contain any shapes! (I think ShapeRange only works properly from the
Selection object). I've tried testing for an error on

Not so.
If I anchor two shapes to the first paragraph in the document, and then use:

Dim myParaRange As Range

Set myParaRange = ActiveDocument.Paragraphs(1).Range

MsgBox myParaRange.ShapeRange.Count

then I get "2" as expected.

If your ShapeRange collection returns 0, then it means there are no shape in
the range.
If you think there should be some, then either they are Inlineshapes, or you
did not set up your range properly.
Range.ShapeRange.Anchor but the same error is generated if there are
no shapes or if there are multiple shapes, so it's not reliable for
what I want to do.

Anchor does not return the anchor itself, but allows you to access the
paragraph in which the Anchor is set.
Normally, we use it to access a paragraph range when we know/have access to
a shape anchored in an unspecified paragraph (Like the first shape in a
document, or access a shape by name)
E.g.

Dim myParaRange As Range

Set myParaRange = ActiveDocument.Shapes(1).Anchor.Paragraphs(1).Range

I thinmk if you start using the online help in the VB editor, you will save
yourself some time and be abler to go further on your own.

In the VB editor, ype
ActiveDocument.Paragraphs(1).ShapeRange(1).Anchor
then slect Anchor and hit F1.

You get:

<quote>
Anchor Property

See Also Applies To Example

Returns a Range object that represents the anchoring range for the specified
shape or shape range. Read-only.
<snip>
<\quote>

So, this tells me that
Dim myParaRange As Range
Dim myShapeTextRange As Range

Set myParaRange = ActiveDocument.Paragraphs(1).Range
Set myShapeTextRange = myParaRange.ShapeRange.Anchor
will fail because I do not specify which range I want from Anchor.

So, if I try
Dim myParaRange As Range
Dim myShapeTextRange As Range

Set myParaRange = ActiveDocument.Paragraphs(1).Range
Set myShapeTextRange = myParaRange.ShapeRange.Anchor.Paragraphs(1).Range
it also fails because I do not specify a particular shape.

This will work
Dim myParaRange As Range
Dim myShapeTextRange As Range

Set myParaRange = ActiveDocument.Paragraphs(1).Range
Set myShapeTextRange =
myParaRange.ShapeRange(1).Anchor.Paragraphs(1).Range

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

James

Thanks guys

ShapeRange.Count is the solution - must have been defining the range wrongly
as you suggest
 

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