Finding the end of a document after adding a Table

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
 
L

Lene Fredborg

The problem seems to be this:
The three lines of code keep the layout tables separate. In order to split
two adjoining tables, you always need a paragraph mark in between.

If you leave out the three lines, a new layout table will be appended to the
end of the preceding layout table. This means that you will still have only
one layout table (but with more rows). Since you start inserting the problem
tables from the first cell of the layout table, your code will each time
start adding problem tables from the first cell of the one and only layout
table – and the result is tables in tables in tables, etc.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
S

StevenM

Lene Fredborg said:
The problem seems to be this:
The three lines of code keep the layout tables separate. In order to split
two adjoining tables, you always need a paragraph mark in between.

Thank you so very much. That makes perfect sense.

Steven Craig Miller
 

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