Working with a nesting table from inside a nested table - switchingrows.

T

TimvG

Imagine you have a table, and nested in each cell is a table. Your
selection is inside one of the nested tables. You want to switch the
row containing your selected table with the row above it. What makes
this a bit tricky is that (it seems) you can't directly identify the
nesting (exterior) cell/row/table.

Below FWIW is the code I came up with. It seems to work fine, but I'm
hoping/expecting there's a more elegant and robust way to do it.

[An additional constraint I was working with was that if your nested
table is already in the top row of the nesting table, it should seem
to the user as if nothing at all happened; e.g. the cursor should stay
in the same spot. Hence the NestingRowIndex function.]

Sub MoveRowUp()
Dim DeleteMe As Row
If NestingRowIndex > 1 Then
Selection.Expand wdTable
Selection.Collapse wdCollapseEnd
Selection.MoveEnd wdCharacter
Selection.Cells(1).Range.Copy
Set DeleteMe = Selection.Rows(1)
Selection.Expand wdRow
Selection.MoveUp
Selection.Rows.Add
Selection.Cells(1).Range.Paste
DeleteMe.Delete
Else
MsgBox ("You're at the top already.")
End If

End Sub

Function NestingRowIndex()
'You're in a nested table. What is the row number of cell in which
the table is nested?
Dim testRange As Range
Set testRange = Selection.Range
testRange.Expand wdTable
testRange.Collapse wdCollapseEnd
testRange.MoveEnd wdCharacter
NestingRowIndex = testRange.Cells(1).RowIndex
End Function


PS my approach was based on a suggestion by Cindy Meister,
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/6197b52a-5424-469a-bd88-1a55b5dd7a79
 

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