Word

Disable "add row" feature in table where "Tab-key" in the last cell adds a new row.

Ease of Use

Intermediate

Version tested with

2003 

Submitted by:

MOS MASTER

Description:

Uses the Word macro ?NextCell? to capture the movement by ?Tab-key? in a table. Programmatically disables the add row feature (?Tab-key?) to create fixed sized tables. 

Discussion:

You would like a table not to add any more rows when you?re in the last cell. This sub makes the cursor loop within the table when you hit the ?Tab-key?. Note: this doesn?t mean you cannot add any rows or columns to the table with the normal methods! It just takes care off the automatic part that the ?Tab-key? does when ?Tabbed? out off the last cell in a table. The list of Word Macros can be seen by pressing Alt +F8 and looking at "Macros in" + "Word Commands" 

Code:

instructions for use

			

Option Explicit Sub NextCell() Dim iActiveColumn As Integer Dim iColumnCount As Integer Dim iActiveRow As Integer Dim iRowCount As Integer 'Get position of cursor iActiveColumn = Selection.Information(wdEndOfRangeColumnNumber) iColumnCount = Selection.Information(wdMaximumNumberOfColumns) iActiveRow = Selection.Information(wdEndOfRangeRowNumber) iRowCount = Selection.Information(wdMaximumNumberOfRows) 'Check if where in last cell If iActiveColumn < iColumnCount Or _ iActiveRow < iRowCount Then 'Not in the last cell so do normal move Selection.MoveRight Unit:=wdCell, _ Count:=1, _ Extend:=wdMove Else 'Yepz in the last cell go to first cel Selection.Tables(1).Cell(1, 1).Select End If End Sub Sub AddATable() Dim oRange As Word.Range Dim oTable As Word.Table Set oRange = ActiveDocument.Bookmarks("bmTable").Range 'Add table to range of bookmark (oRange) Set oTable = ActiveDocument.Tables.Add(Range:=oRange, _ NumRows:=3, _ NumColumns:=3, _ AutoFitBehavior:=wdAutoFitFixed) 'Add special table format oTable.AutoFormat Format:=wdTableFormatColorful2 oTable.Cell(1, 1).Select Set oRange = Nothing Set oTable = Nothing End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Paste the code there in the window at right. (F7)
  5. Close VBE (Alt + Q or press the X in the top right hand corner).
  6. Save the file.
 

Test the code:

  1. The NextCell macro will run automatically if you press the "tab-key" from within a Word table.
  2. To run the AddATable sub:
  3. From Word, press Alt + F8 to open the macro dialog box.
  4. Select AddATable
  5. Click Run.
 

Sample File:

Next Cell macro.zip 11.74KB 

Approved by mdmackillop


This entry has been viewed 60 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express