#1   Report Post  
Posted to microsoft.public.word.docmanagement
owen owen is offline
external usenet poster
 
Posts: 61
Default VBA Code Help Required

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
  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default VBA Code Help Required

On Sun, 25 Jan 2009 16:32:01 -0800, Owen wrote:

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/comm....vba.general.]


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help required Bryan Microsoft Word Help 1 August 1st 08 03:36 PM
Can Word 2007 apply a zip code bar code to envelopes? LINDA Microsoft Word Help 4 January 5th 08 04:45 AM
Help required ??? goldenman Microsoft Word Help 0 January 25th 06 12:17 PM
Microworks asks for a code tahat I do not know. How do I get code Francisco Microsoft Word Help 1 November 23rd 05 09:01 PM
Help required..... kiran Microsoft Word Help 0 September 1st 05 02:10 PM


All times are GMT +1. The time now is 02:58 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"