How do you reference a table?

  • Thread starter Jennifer Robertson
  • Start date
J

Jennifer Robertson

In the VBA reference it refers to collections, which I understand.

Objects in collections have properties. Thus, you have some sort of
properties dialogue as with a field or other control.

Table properties in Word just deal the style and formatting of the
table. How does one reference a simple table in Word VBA?
 
D

Doug Robbins - Word MVP

Dim myTable as Table
Set myTable =ActiveDocument.Tables(1)

with create a reference myTable to the first table in the document. Or, if
the selection is in the table to which you want to be able to make reference

Set myTable = Selection.Tables(1)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
F

Fumei2 via OfficeKB.com

"Table properties in Word just deal the style and formatting of the
table. "

That is true, but as Doug shows how, if you make a table OBJECT then you have
access to all object properties of that table.

Once you have the object (as with Doug's code), you can:

Put text into a give cell:
MyTable.Cells(3,2).Range.Text = "yadda yadda"

Make a CELL object, and do stuff with thatt:
Dim oCell As Cell
,
other stuff...
' set the cell object to the third cell of the table
' note this is NOT by row/column!
Set oCell = MyTable.Range.Cell(3)

' see the Function CellText2 below
If CellText2(oCell.Range.Text) = "Blah blah" Then
oCell.Range.Text = "whopppeeee!"
End If

And anything else that is exposed to VBA for a table...which is a LOT.

N.B. be careful about getting text out of cells. The cell range includes
the end-of-cell marker. To get only the text use one of these functions.

Function CellText(oCell As Cell)
CellText = Left(oCell.Range.Text, _
Len(oCell.Range.Text) - 2)
End Function

Function CellText2(strIn As String)
CellText2 = Left(strIn, Len(strIn) - 2)
End Function

The first passes the cell itself as the parameter. The second passes the
range.text (including the end-of-cell marker).

Which to use depends on whether it is useful to have cell objects, or not.
 
J

Jennifer Robertson

Thanks for the replies....

Soooo..... if I have a word form with tables already in it, I just
reference the table as it falls within the document by index.

By creating the object, I'm just creating the reference to the table
that is already in my document. I'm not actually creating a table?
 
D

Doug Robbins - Word MVP

Yes, that is pretty much the way it is.

You can however create a table at the same time that you assign it to an
object using

Dim mytable as Table
Set mytable = ActiveDocument.Tables.Add(Selection.Range, 2, 2)

would create a 2 x 2 table at the location of the selection in the
activedocument and at the same time, create the reference (mytable) to it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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