1. How do I lock my bottom table row from adding more lines when I try to
tab to the next table.
2. If necessary, how do I merge two tables?
Thank you VERY much!
For the first question, you can't "lock" the table row, but you can
install this macro in your template (see
http://www.gmayor.com/installing_macro.htm if needed) to change the
behavior of the tab key when the cursor is in the last cell of a
table:
Sub NextCell()
Dim oTbl As Table
Dim tblIndex As Long
Dim thisRow As Long, thisCol As Long
Set oTbl = Selection.Tables(1)
tblIndex = ActiveDocument.Range(0, oTbl.Range.End).Tables.Count
thisRow = Selection.Information(wdEndOfRangeRowNumber)
thisCol = Selection.Information(wdEndOfRangeColumnNumber)
If Not ((thisRow = oTbl.Rows.Count) And _
(thisCol = oTbl.Columns.Count)) Then
Selection.MoveRight Unit:=wdCell, Count:=1
Else
' If this isn't the last table, jump to the next table
If tblIndex < ActiveDocument.Tables.Count Then
ActiveDocument.Tables(tblIndex + 1).Cell(1, 1).Select
Selection.Collapse wdCollapseStart
End If
End If
End Sub
The other thing is that normally you can't "tab to the next table"
unless you have a protected form with form fields in the table cells.
But the above macro will take care of that, too.
For your second question, just delete any text and paragraph marks
from between the two tables to merge them. This is easier to do if you
can see the paragraph marks, so click the � button to turn them on and
off.
--
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.
For question 1, you can install the following macro in the template for this
kind of document (see http://www.gmayor.com/installing_macro.htm if needed):
Sub NextCell()
Dim thisRow As Integer, thisCol As Integer
Dim thisTable As Table, iTbl As Table
Dim thisTableIdx As Long
Set thisTable = Selection.Tables(1)
thisTableIdx = ActiveDocument.Range(0, _
thisTable.Range.End).Tables.Count
thisRow = Selection.Information(wdEndOfRangeRowNumber)
thisCol = Selection.Information(wdEndOfRangeColumnNumber)
If Not ((thisRow = thisTable.Rows.Count) And _
(thisCol = thisTable.Columns.Count)) Then
Selection.MoveRight Unit:=wdCell
Else
If thisTableIdx < ActiveDocument.Tables.Count Then
' there is another table to go to
ActiveDocument.Tables(thisTableIdx + 1).Cell(1, 1).Select
Selection.Collapse wdCollapseStart
End If
End If
End Sub
Note that normally (unless you're working in a protected form document and
there is a form field in the next table) you can't "tab to the next table".
The macro makes that possible.
For question 2, just delete the paragraph mark(s) between the tables to make
them into one table. This is easier to control if you can see the paragraph
marks; clicking the � button switches them on and off.
Say, Jay, I adapted this code to something I'm doing and I've got a
problem. My table is two columns, and in the last two rows the cells
in the right column are merged:
+----+-----+
| A | |
+----+ B |
| C | |
+----+-----+
So the Selection.MoveRight Unit:=wdCell moves the selection from Cell
A to Cell B, then to Cell C, then back to Cell B. Cell A is at
(r1,c1), B is at (r1,c2), and C is at (r2,c1). There is no cell that
has the maximum row count and the maximum column count. That is, there
is no cell at (r2,c2). So the condition in the snippet above is true
for cell B both times the selection hits it, and on the second time,
the MoveRight creates a new row.
Any way to prevent that new row from being created?
TIA
---larry
You would also want to start the event handler with a check for
whether the ActivDocument.AttachedTemplate is the one that contains
the table; if not, immediately Exit Sub. That will take care of the
possibility that a document based on your template and one based on a
different template are open at the same time (because the event
handler fires for all documents).
--
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.
Thanks, Jay. As it happens I don't know the exact shape of the table;
the macro has to work on any shape. So instead of using a MoveRight
command, I've changed my approach to use a two-dimensional array to
capture values for cells. In the following sample code, I'm capturing
the vertical alignment and background colour of cells prior to
changing the table style to Table Normal (which clobbers those two
attributes, among many others). Then I can restore those attributes
cell by cell. It's working fine for my purposes, and sidesteps the
issue of accidentally creating new rows.
---larry
Sub CTS()
'''''''''
On Error GoTo ErrMsg:
If Not Selection.Information(wdWithInTable) Then Exit Sub
Dim myTbl As Table
Dim myRow As Long
Dim myCol As Long
Dim maxRow As Long
Dim maxCol As Long
Set myTbl = Selection.Tables(1)
maxRow = myTbl.Rows.Count
maxCol = myTbl.Columns.Count
'''\ Set a two-dimensional array.
' Unfortunately, we can't set the values _
using variables, but we also can't set _
a multidimensional array without values, _
and in any case we can't ReDim both _
dimensions to variable values. So we _
just have to set fixed values and hope _
that they're big enough.
Dim cellColour(100, 100) As Double
Dim cellVertAlign(100, 100) As Double
'''/
On Error GoTo NoCell1:
For myRow = 1 To maxRow
For myCol = 1 To maxCol
cellColour(myRow, myCol) = _
myTbl.Cell(myRow, myCol).Shading.BackgroundPatternColor
cellVertAlign(myRow, myCol) = _
myTbl.Cell(myRow, myCol).VerticalAlignment
NoCell1: ' If a cell doesn't exist (because of merging)
Next
Next
On Error GoTo ErrMsg:
With myTbl
.Style = "Table Normal"
.ApplyStyleHeadingRows = False
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = False
.ApplyStyleLastColumn = False
End With
On Error GoTo NoCell2:
For myRow = 1 To maxRow
For myCol = 1 To maxCol
myTbl.Cell(myRow, myCol).Shading.BackgroundPatternColor = _
cellColour(myRow, myCol)
myTbl.Cell(myRow, myCol).VerticalAlignment = _
cellVertAlign(myRow, myCol)
NoCell2: ' If a cell doesn't exist (because of merging)
Next
Next
On Error GoTo ErrMsg:
GoTo Bye:
ErrMsg:
MsgBox "Whatever",,"ERROR"
Bye:
'''''''
End Sub