Can I control navigation from one cell to another?

C

CW

1. Is it possible to program a table cell so that upon exit from it, the
cursor will go to another particular specified cell?
2. Can I control whether the cursor moves downwards rather than to the right
upon cell exit (as is possible in Excel)?
 
J

Jay Freedman

CW said:
1. Is it possible to program a table cell so that upon exit from it,
the cursor will go to another particular specified cell?
2. Can I control whether the cursor moves downwards rather than to
the right upon cell exit (as is possible in Excel)?

If you're working in a protected form built in a table, then see
http://word.mvps.org/FAQs/TblsFldsFms/SetTabOrder.htm. The macros there work
entirely on the principles of form fields, their bookmark names, and their
exit macros -- the fact that the fields are in a table is purely
coincidental and not necessary.

Outside of a protected form, working with a bog-standard table, it would be
*possible* but difficult and error-prone to write a macro to look at which
cell contains the cursor and move accordingly. How would the macro "know"
which cell should have the special behavior and where the "next" cell is?
What if the user adds or deletes rows, merges cells, or does any of the
strange and wonderful things that Word allows? Bookmarked cells are a
possibility, but bookmarks are fragile and easily deleted.

To make the cursor move downward in *all* tables (at least ones that don't
contain merged or split cells, which give VBA fits), you can install this
macro. To restrict it to documents based on a specific template, store the
macro in that template; to have it work in all documents, store it in
Normal.dot or a global template in the Startup folder.

Sub NextCell()
Dim numRows As Long, numCols As Long
Dim thisRow As Long, thisCol As Long

' shouldn't need this, but I'm paranoid...
If Not Selection.Information(wdWithInTable) Then
Exit Sub
End If

' some of this stuff fails if the table
' contains any merged or split cells
On Error GoTo oops

numRows = Selection.Tables(1).Rows.Count
numCols = Selection.Tables(1).Columns.Count
thisRow = Selection.Information(wdStartOfRangeRowNumber)
thisCol = Selection.Information(wdStartOfRangeColumnNumber)

If (thisRow = numRows) And (thisCol = numCols) Then
' this is the bottom right cell...
' do the built-in behavior
Selection.Tables(1).Rows.Add
Selection.Tables(1).Cell(thisRow + 1, 1).Select
Else
If (thisRow = numRows) Then
' go to top of next column
Selection.Tables(1).Cell(1, thisCol + 1).Select
Else
' move down one cell
Selection.Tables(1).Cell(thisRow + 1, thisCol).Select
End If
End If

Exit Sub
oops:
MsgBox "Macro can't work with merged/split table cells", _
vbOKOnly + vbCritical, "Can't do it..."
End Sub
 
T

Tony Jollans

There is nothing built-in but it can be done with code.You need to code a
macro called NextCell which will automatically run in response to pressing
Tab in a table cell, and which can determine what should be done according
to whatever criteria you choose.
 
C

CW

Many thanks, Jay and Tony - I'll have a go with the macro - sounds promising.
(I love the inclusion of the "oops" bit!!!)
 
C

CW

Many thanks, Jay and Tony - I'll have a go with the macro - sounds promising.
(I love the inclusion of the "oops" bit!!!)
 

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