<!-- [if gte mso 10]> <mce:style>
Let’s start with some background before I get into my issue.
In the template that I’m modifying for my colleagues, there is a 2 column, multi-row table that they will fill in. Content in the left column (Column A) are categories, and content in the right column (Column B) are the elements that make up a category.
Each element for a category is in a cell, and the empty cells in the category column (for a given category) are merged into a single cell. The number of elements will vary for a category. Elsewhere in the document, there will be a table for each of these categories,
with their elements included in that table (the purpose is to expound on the elements).
I wrote a macro whose intent is to take the contents from the first table and automatically create these individual category tables. Below is the VBA code that I am using to determine the number of elements per category (and also where I will have my issue):
************
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Category")
. ==> This section trimmed for brevity
End With
Selection.Copy
TableRowStart = Selection.Cells(1).RowIndex
Selection.MoveRight
ActiveDocument.Bookmarks.Add Name:="temp", Range:=Selection.Range
Selection.MoveDown
If Selection.Information(wdWithInTable) = True Then
TableRowEnd = Selection.Cells(1).RowIndex
ElementCount = TableRowEnd - TableRowStart
Else
Selection.MoveStart Unit:=wdLine, Count:=-1
Selection.MoveLeft
Selection.MoveLeft
TableRowEnd = Selection.Cells(1).RowIndex
ElementCount = (TableRowEnd - TableRowStart) + 1
MacroRun = "False"
End If
**********
At first I thought I was golden until I put the table into this situation: Near the bottom of a page, I have Category X with multiple elements. Some of these elements (e.g. 3) are at the bottom of the page and the remaining elements are at the top of the
next page (a common occurrence with this document).
What happens is that with the “Selection.MoveDown” command, the selection point moves to the element row at the top of the page instead of the next Category row or outside the table (I can also reproduce this behavior using the down cursor key).
I’ve tried replacing this command with “Selection.MoveDown Unit:=wdRow” and “Selection.MoveDown Unit:=wdCell”, but get a runtime error. Using the “Selection.Move Unit:=wdRow” command moves the selection point to the front of the starting row.
I will appreciate any ideas on how I can move down a row in this scenario and still stay in column A (or exit the table if it’s the last category item).