VBA Code Help Required

O

Owen

I have a word document with a series of tables. I am trying to determine the
code required to place the cursor within the last field in a specified table.

In my code below, the cursor is positioned in the first field of Table 7 in
my document.

Selection.GoTo What:=wdGoToTable, Which:=wdGoToAbsolute, Count:=7

Can anyone help me with the coding required to then position the cursor in
the last field of the specified table?

Thanks
 
J

Jay Freedman

I have a word document with a series of tables. I am trying to determine the
code required to place the cursor within the last field in a specified table.

In my code below, the cursor is positioned in the first field of Table 7 in
my document.

Selection.GoTo What:=wdGoToTable, Which:=wdGoToAbsolute, Count:=7

Can anyone help me with the coding required to then position the cursor in
the last field of the specified table?

Thanks

Although you may be able to torture Selection.GoTo until it does what you want,
it's easier to think in terms of objects instead of keystrokes:

Within the active document, you want the seventh table. In VBA's object-model
language, that's expressed as ActiveDocument.Tables(7). You're going to refer to
that table several times to get where you want, so let's assign it to an object
variable:

Dim myTable As Table
Set myTable = ActiveDocument.Tables(7)

Within that table, you want the last cell in the last row. The table object has
a .Cell method; when you pass it the row and column numbers, you get back a cell
object for the cell at those coordinates. The row number of the last row is
myTable.Rows.Count, and the column number of the last column is
myTable.Columns.Count. [But see below for more info.] So the cell you want is

myTable.Cell(myTable.Rows.Count, myTable.Columns.Count)

To select that cell, you call the .Select method of the cell object:

myTable.Cell(myTable.Rows.Count, myTable.Columns.Count).Select

That selects the whole cell, including any existing text and the invisible cell
marker. If you want the cursor to just be a flashing insertion point at the
beginning of the cell, you need one more statement:

Selection.Collapse Direction:=wdCollapseStart

Summing up, the macro code you should use is

Dim myTable As Table
Set myTable = ActiveDocument.Tables(7)
myTable.Cell(myTable.Rows.Count, myTable.Columns.Count).Select
Selection.Collapse Direction:=wdCollapseStart

[The additional info: The last cell of the last row is given by the two .Count
numbers only if the table is rectangular, and doesn't contain merged or split
cells. Word lets you get much more creative than that, but VBA has a very hard
time dealing with it. If you need to handle that situation, post in
http://www.microsoft.com/office/community/en-us/default.mspx?dg=microsoft.public.word.vba.general.]
 

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