Why doesn't the Table object have an Index property

R

Raphael Goubet

Hi,

I'm wondering why the Table object does not have an Index property?
This object belongs to a collection (Tables), so it should have one.

What is the reason this object, just like Document or Paragraph does
not have that property?

Thanks in advance.

Raph
 
P

Peter Hewett

Hi Raphael Goubet

Strangely tables and numerous other objects don't have an index property. However, there
is a standard word around to this problem:

Public Function TableIndex() As Long
Dim rngToTable As Word.Range

' No table in selection then nothing to return
If Selection.Tables.Count > 0 Then

' Set range to first table of selection
Set rngToTable = Selection.Tables(1).Range
rngToTable.MoveStart wdStory, -1

' Return the tables effective document index
TableIndex = rngToTable.Tables.Count
End If
End Function

HTH + Cheers - Peter


(e-mail address removed) (Raphael Goubet), said:
 
J

Jezebel

It's a result of the standard way Collections are managed in VB and VBA.
Each member of a collection has an Index and optionally a Key; however these
are properties of the *Collection* not of the item. The index is determined
by the item's current position in the collection, and changes if preceding
items are added or removed -- as you see happening with most of Word's
collections, like Tables(), Paragraphs(), Words().

The Index is not a property of the item itself because the item has no
control over its storage. You can have multiple collections containing the
same type of object; and one object can be a member of more than one
collection. In the case of a Table, if it had an Index property how would
you know if the index referred to its position within

ActiveDocument.Tables
ActiveDocument.Bookmark("MyBookMark").Tables
ActiveDocument.Application.Selection.Tables
Activedocument.Sections(2).Range.Tables

etc? The same table might be a member of all of these, with a different
index in each case.
 
R

Raphael Goubet

That's a good trick to know, thanks.

Although I don't have a clue how it works! :)

Raphael
 

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