Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
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
Posted to microsoft.public.word.docmanagement
|
|||
|
|||
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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Help required | Microsoft Word Help | |||
Can Word 2007 apply a zip code bar code to envelopes? | Microsoft Word Help | |||
Help required ??? | Microsoft Word Help | |||
Microworks asks for a code tahat I do not know. How do I get code | Microsoft Word Help | |||
Help required..... | Microsoft Word Help |