PDA

View Full Version : [SOLVED:] Cut and Paste Nested Tables



gmaxey
08-02-2015, 03:26 PM
Today I have fallen off the turnip truck. I can't seem to work anything out.

I have 2 X 3 parent table with a 1 x 2 nested table in cells 1, 2 and 3. I can select, cut and paste the nested table in cell 3 into cell 4 using the mouse, but nothing I've tried with VBA works. I either end up with the table pasted BETWEEN rows 1 and 2. Or I just get the text of the nested table (no cells, borders, etc.) in cell 4.


Sub CutAndPasteNestedTable()
Dim oTblParent As Table
Dim oTblToCut As Table
Dim oRng As Range
'I have a simple 2 X 3 table with a sime 1 X 2 nested table in cell 3.
'I want to cut the nested table from cell 3 and paste it in cell 4.
Set oTblParent = ActiveDocument.Tables(1)
Set oRng = oTblParent.Range.Cells(3).Range
oRng.End = oRng.End - 2
'I 've got then nested table.
Set oTblToCut = oRng.Tables(1)
oTblToCut.Range.Cut
'The table is now in the clipboard. If I end, I can select the start of
'cell 4 and paste the table cut from cell 3.
'End
'However, if I try to do that programatically, the table cut is pasted in between the table rows 1 and 2
Set oRng = oTblParent.Range.Cells(4).Range
oRng.Collapse wdCollapseStart
oRng.Paste
End Sub

gmaxey
08-06-2015, 03:22 AM
I found a solution:


Sub CutAndPasteNestedTable()
Dim oTblParent As Table
Dim oTblToCut As Table
Dim oRng As Range
Set oTblParent = ActiveDocument.Tables(1)
Set oRng = oTblParent.Range.Cells(3).Range
oRng.End = oRng.End - 2
Set oTblToCut = oRng.Tables(1)
oTblToCut.Range.Cut
Set oRng = oTblParent.Range.Cells(4).Range
oRng.Collapse wdCollapseStart
oRng.PasteAsNestedTable 'This was the illusive method I needed.
End Sub

StepKor
12-18-2015, 02:52 PM
Greg, thank you so much! You just saved my day, mental health too. :) Range.Paste didn't work, Selection.Paste didn't work, Table.Range.Cells(i).Range.Paste didn't work... And so hours flew by.
The most amazing thing is that I was initially looking for a method like this and was almost certain it existed. Yet I was unable to find it though I was certainly skimming through the list of available methods in VBE. As soon as I found your post I checked again... and there it was, surprisingly. :)

Thanks again, it's one of those nagging Word-related "small" problems that are quite capable of driving you completely nuts.

gmaxey
12-18-2015, 05:33 PM
Sometimes we are not alone amongst our bits of hair and bloody scalp. Glad my horror story with a happy ending helped add a happy ending to yours.