Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2006

Tip: Looking for answers? Try searching our database.

Detect shapes anchored in paragraph

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James - 21 Apr 2006 12:43 GMT
Hi

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

Thanks
Signature

James

Jezebel - 21 Apr 2006 13:30 GMT
Range.Shapes.Count > 0

> Hi
>
[quoted text clipped - 3 lines]
>
> Thanks
James - 21 Apr 2006 13:41 GMT
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)
Signature

James

> Range.Shapes.Count > 0
>
[quoted text clipped - 5 lines]
> >
> > Thanks
Jezebel - 21 Apr 2006 15:22 GMT
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.

>I don't think you can get the shapes collection from a range object. You
>can
[quoted text clipped - 14 lines]
>> >
>> > Thanks
James - 21 Apr 2006 16:29 GMT
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.
Signature

James

> Why not try it?
>
[quoted text clipped - 20 lines]
> >> >
> >> > Thanks
Tony Jollans - 21 Apr 2006 21:18 GMT
Try Range.ShapeRange.Count

--
Enjoy,
Tony

> 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
[quoted text clipped - 36 lines]
> > >> > --
> > >> > James
Jean-Guy Marcil - 22 Apr 2006 00:27 GMT
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

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

James - 23 Apr 2006 13:38 GMT
Thanks guys

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

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
[quoted text clipped - 30 lines]
> > >> >
> > >> > Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.