For all the collections, particularly tables, are the collections
dynamically reordered?
So, if the first table is index number one, and I add a new table in
front of it, does the new one become number one and the rest renumbered
in order from top to bottom?
If we have tables two and three and a new one is put inbetween, will
the new one become three and the one previously three become four?
Is this true for all collections that reflect an order in a document?
Like characters, words, etc?
Thanks.
Collections are *usually* dynamically renumbered, but the
implementation isn't necessarily consistent across all collections,
and I would never depend on this behavior.
If you must have the index of a particular object, see
http://www.word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm.
Usually, though, it's better to assign the desired table, paragraph,
or whatever to an object variable of the correct type, and then
operate on that object. Then you never need to know its absolute
index. For example:
Sub AddTable()
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables.Add( _
Range:=Selection.Range, _
numrows:=3, numcolumns:=2)
With oTbl
.Cell(1, 1).Range.Text = "A"
.Cell(1, 2).Range.Text = "B"
.Rows(1).Range.ParagraphFormat.Alignment = _
wdAlignParagraphCenter
End With
End Sub
Be especially wary of using index numbers in a For loop that may
delete one or more of the objects in the collection. You can easily
get into a situation where the indexing of the collection is out of
synch with the loop counter. If you need to delete all the members of
the collection, delete item 1 as many times as the starting size of
the collection:
Sub DeleteAllTables()
Dim idx As Long
For idx = 1 To ActiveDocument.Tables.Count
ActiveDocument.Tables(1).Delete
Next idx
End Sub
To delete only some of the members, assign each one in turn to an
object variable and then call .Delete on that object.
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.