S
StevenM
The following code is a mock-up of code which I have implemented in my
project. It creates a "layout" table (here with 3 rows and 3 columns) and
then places a "problem" table in each cell of the "layout" table. In the
actual version, each "layout" table represents a page (with the rows and
columns variable). The next layout table being on the next page. In
implementing this code in my actual project I discovered (after sometime
debugging) that there are three lines of code at the end which if omitted
makes everything erratic. I wonder if someone could tell me why?
In other words, I found my problem, but I'm having a hard time understanding
why it was a problem. My guess is that without adding a paragraph mark after
the layout table, I'm unable to reset my range variable to the next layout
table.
Sub TestTableLayout()
Dim problemRange As Range
Dim oRange As Range
Dim layoutTable As Table
Dim problemTable As Table
Dim nMaxRows As Long
Dim nMaxCols As Long
Dim nLoRow As Long
Dim nLoCol As Long
Dim nIndex As Long
Dim nExtraRow As Long
Dim nRow As Long
nMaxLoRows = 3
nMaxLoCols = 3
nLoRow = nMaxLoRows
nLoCol = nMaxLoCols
Set problemRange = ActiveDocument.Range
'****
'* Run a test for 20 problems
'****
For nIndex = 1 To 20
'****
'* If Layout Table Is Full (or First Time), Add New Layout Table
'****
If nLoRow = nMaxLoRows And nLoCol = nMaxLoCols Then
Set layoutTable = problemRange.Tables.Add(Range:=problemRange,
NumRows:=nMaxLoRows, NumColumns:=nMaxLoCols)
nLoRow = 1
nLoCol = 0
With layoutTable
.Borders.Enable = False
End With
End If
'****
'* Move to Next Layout Table Cell
'****
If nLoCol = nMaxLoCols Then
nLoCol = 1
nLoRow = nLoRow + 1
Else
nLoCol = nLoCol + 1
End If
Set oRange = layoutTable.Cell(nLoRow, nLoCol).Range
oRange.Collapse
'****
'* Create New Problem Table
'****
If nLoRow = nMaxLoRows Then
nExtraRow = 0
Else
nExtraRow = 1
End If
Set problemTable = oRange.Tables.Add(Range:=oRange, NumRows:=(3 +
nExtraRow), NumColumns:=1)
problemTable.Borders.Enable = False
nRow = 1
With problemTable.Cell(nRow, 1).Range
.Text = "Table " & nIndex & " Row " & nRow
End With
nRow = nRow + 1
With problemTable.Cell(nRow, 1).Range
.Text = "Table " & nIndex & " Row " & nRow
End With
nRow = nRow + 1
With problemTable.Cell(nRow, 1).Range
.Text = "Table " & nIndex & " Row " & nRow
End With
If nExtraRow = 1 Then
With problemTable.Cell(nRow + nExtraRow, 1).Range
.Text = ""
End With
End If
'****
'* If Layout Table Is Full, Prepare For New Layout Table
'****
If nLoRow = nMaxLoRows And nLoCol = nMaxLoCols Then
Set problemRange = layoutTable.Range
With problemRange
.MoveEnd wdCharacter, 1
.Collapse wdCollapseEnd
'****
'* Why are the three following lines necessary?
'* Remove the quote mark before the line to see it work properly.
'* Then add the quote mark back to see the code act erratically.
'****
'.InsertParagraphAfter
'.MoveEnd wdCharacter, 1
'.Collapse wdCollapseEnd
End With
End If
Next nIndex
End Sub
project. It creates a "layout" table (here with 3 rows and 3 columns) and
then places a "problem" table in each cell of the "layout" table. In the
actual version, each "layout" table represents a page (with the rows and
columns variable). The next layout table being on the next page. In
implementing this code in my actual project I discovered (after sometime
debugging) that there are three lines of code at the end which if omitted
makes everything erratic. I wonder if someone could tell me why?
In other words, I found my problem, but I'm having a hard time understanding
why it was a problem. My guess is that without adding a paragraph mark after
the layout table, I'm unable to reset my range variable to the next layout
table.
Sub TestTableLayout()
Dim problemRange As Range
Dim oRange As Range
Dim layoutTable As Table
Dim problemTable As Table
Dim nMaxRows As Long
Dim nMaxCols As Long
Dim nLoRow As Long
Dim nLoCol As Long
Dim nIndex As Long
Dim nExtraRow As Long
Dim nRow As Long
nMaxLoRows = 3
nMaxLoCols = 3
nLoRow = nMaxLoRows
nLoCol = nMaxLoCols
Set problemRange = ActiveDocument.Range
'****
'* Run a test for 20 problems
'****
For nIndex = 1 To 20
'****
'* If Layout Table Is Full (or First Time), Add New Layout Table
'****
If nLoRow = nMaxLoRows And nLoCol = nMaxLoCols Then
Set layoutTable = problemRange.Tables.Add(Range:=problemRange,
NumRows:=nMaxLoRows, NumColumns:=nMaxLoCols)
nLoRow = 1
nLoCol = 0
With layoutTable
.Borders.Enable = False
End With
End If
'****
'* Move to Next Layout Table Cell
'****
If nLoCol = nMaxLoCols Then
nLoCol = 1
nLoRow = nLoRow + 1
Else
nLoCol = nLoCol + 1
End If
Set oRange = layoutTable.Cell(nLoRow, nLoCol).Range
oRange.Collapse
'****
'* Create New Problem Table
'****
If nLoRow = nMaxLoRows Then
nExtraRow = 0
Else
nExtraRow = 1
End If
Set problemTable = oRange.Tables.Add(Range:=oRange, NumRows:=(3 +
nExtraRow), NumColumns:=1)
problemTable.Borders.Enable = False
nRow = 1
With problemTable.Cell(nRow, 1).Range
.Text = "Table " & nIndex & " Row " & nRow
End With
nRow = nRow + 1
With problemTable.Cell(nRow, 1).Range
.Text = "Table " & nIndex & " Row " & nRow
End With
nRow = nRow + 1
With problemTable.Cell(nRow, 1).Range
.Text = "Table " & nIndex & " Row " & nRow
End With
If nExtraRow = 1 Then
With problemTable.Cell(nRow + nExtraRow, 1).Range
.Text = ""
End With
End If
'****
'* If Layout Table Is Full, Prepare For New Layout Table
'****
If nLoRow = nMaxLoRows And nLoCol = nMaxLoCols Then
Set problemRange = layoutTable.Range
With problemRange
.MoveEnd wdCharacter, 1
.Collapse wdCollapseEnd
'****
'* Why are the three following lines necessary?
'* Remove the quote mark before the line to see it work properly.
'* Then add the quote mark back to see the code act erratically.
'****
'.InsertParagraphAfter
'.MoveEnd wdCharacter, 1
'.Collapse wdCollapseEnd
End With
End If
Next nIndex
End Sub