ShapeRange gives error

K

Ken

Can anyone tell me why the statement
i = Selection.Range.ShapeRange.Count
gives error message 5852 if there are no shapes in the selection but
works if there are one or more shapes.
I can work around the problem using error trapping but it is messy.

I am using Word2000 VBA
 
J

Jonathan West

Ken said:
Can anyone tell me why the statement
i = Selection.Range.ShapeRange.Count
gives error message 5852 if there are no shapes in the selection but
works if there are one or more shapes.
I can work around the problem using error trapping but it is messy.

I am using Word2000 VBA

Just the way it was programmed. The error checking is pretty simple, this
will do the trick

i = 0
On Error Resume Next
i = Selection.Range.ShapeRange.Count
On Error Goto 0

Not all that messy really.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jean-Guy Marcil

Ken was telling us:
Ken nous racontait que :
Can anyone tell me why the statement
i = Selection.Range.ShapeRange.Count
gives error message 5852 if there are no shapes in the selection but
works if there are one or more shapes.
I can work around the problem using error trapping but it is messy.

I am using Word2000 VBA

I just tried

Dim i As Long

i = Selection.Range.ShapeRange.Count

with and without any shapes in the current selection without any problems. I
either got 0 or the number of shapes.

What else is your code doing?
What is the selection when you run this code?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
K

Ken

I have found another curious thing. If there a shape anywhere else in
the document but none in the selection then the ShapeRange.Count works
and gives a count of 0.
therefore my coding is:

i = 0
If AvtiveDocument.shapes.count > 0 then
i = Selection.Range.ShapeRange.count
end if
 
B

Bear

Ken:

Here's my understanding of things. If I'm wrong, I hope someone will correct
me.

I think your code attempts to access a property of a collection (the Count
property of the ShapeRange collection). The error is telling you that VBA
can't access that property because the collection doesn't exist.

There's an Exists method for some collections that will tell you whether or
not a given member exists, but I don't know if there's a way other than by
error trapping to determine whether or not a collection exists.

Bear
 
Top