Tables Question

M

Mike

Hi,

I have created a template form that has a 5 tables inside it. In VBA code
how can I distguinuish between all of the tables?
 
J

Jay Freedman

Mike said:
Hi,

I have created a template form that has a 5 tables inside it. In VBA
code how can I distguinuish between all of the tables?

Hi Mike,

That depends on what you want to do in the code.

If you know which table you want to work on, just use the index number --
ActiveDocument.Tables(1) is the first table in the document,
ActiveDocument.Tables(2) is the second, and so on.

If you have to do something with the table that currently contains the
cursor, that's always Selection.Tables(1) independent of the indexing within
the document. But if you really need to know the index of the current table
within the document, see
http://word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm.

If you want to loop through all the tables, use a For Each loop:

Dim oTable As Table
For Each oTable In ActiveDocument.Tables
' do something with oTable
Next oTable
 
S

Stephanie Krieger

Hi, Mike,

The tables in your document are numbered in the order
they appear -- their index (when using the Tables
collection object) is their order in the document:

So, for example, if you want to place the text "Word
Tables are Great!" in the cell that sits in the 3rd
column, 4th row of the 2nd table in the document, this
would be the line of code:

ActiveDocument.Tables(2).Rows(4).Cells(3).Range.Text
= "Word Tables are Great!"

Note that, if you have nested tables, or any of the
tables you want to access are not in the main story of
the document (such as in the Header\Footer layer or in a
footnote) ...there's a bit more to it than this ... so
let me know if that's applicable.

If you want to just confirm that Word sees the table you
think is table 3,for example, as table 3 ... run this
line of code:

ActiveDocument.Tables(3).Select

The table Word sees as table 3 will be selected.

Hope that helps!

Stephanie Krieger
author of Microsoft Office Document Designer (from
Microsoft Press)
email: MODD_2003 at msn dot com
blog: arouet.net
 

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