none
Select nested tables only. RRS feed

  • Question

  • I have main table with tens of rows, each row contains a nested table, I want either to select only these nested tables to copy or cut and paste in a new page, or to copy the main table and paste in a new page and to keep the nested tables only (paste as "keep text only" will convert all tables into text). Any idea. Thanks
    Saturday, September 28, 2013 6:05 PM

Answers

  • Well, as it's not possible to make multiple, non-contiguous selections using the Word APIs, what you propose is not possible to achieve in the way you propose to procede.

    I think your code will need to loop through each row of the main table, address the nested table, then transfer it to your target range. Preferably without using copy/paste. Something like the following VBA code:

    Sub ExtractNestedTables()
        Dim tbl As word.Table, tblNested As word.Table
        Dim rng As word.Range
        Dim rowCounter As Long, rw As word.Row
        
        Set tbl = ActiveDocument.Tables(1)
        Set rng = Selection.Range
        If Not rng.Information(wdWithInTable) Then
            For rowCounter = 1 To tbl.Rows.Count
                Set rw = tbl.Rows(rowCounter)
                If rw.Cells(1).Tables.Count > 0 Then
                    Set tblNested = rw.Cells(1).Tables(1)
                    rng.FormattedText = tblNested.Range.FormattedText
                    rng.Collapse wdCollapseEnd
                    rng.InsertAfter vbCr
                    rng.Collapse wdCollapseEnd
                End If
            Next
        End If
    End Sub
    


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Ahmed Morsyy Friday, October 4, 2013 7:08 AM
    Monday, September 30, 2013 6:13 PM
    Moderator

All replies

  • Well, as it's not possible to make multiple, non-contiguous selections using the Word APIs, what you propose is not possible to achieve in the way you propose to procede.

    I think your code will need to loop through each row of the main table, address the nested table, then transfer it to your target range. Preferably without using copy/paste. Something like the following VBA code:

    Sub ExtractNestedTables()
        Dim tbl As word.Table, tblNested As word.Table
        Dim rng As word.Range
        Dim rowCounter As Long, rw As word.Row
        
        Set tbl = ActiveDocument.Tables(1)
        Set rng = Selection.Range
        If Not rng.Information(wdWithInTable) Then
            For rowCounter = 1 To tbl.Rows.Count
                Set rw = tbl.Rows(rowCounter)
                If rw.Cells(1).Tables.Count > 0 Then
                    Set tblNested = rw.Cells(1).Tables(1)
                    rng.FormattedText = tblNested.Range.FormattedText
                    rng.Collapse wdCollapseEnd
                    rng.InsertAfter vbCr
                    rng.Collapse wdCollapseEnd
                End If
            Next
        End If
    End Sub
    


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Ahmed Morsyy Friday, October 4, 2013 7:08 AM
    Monday, September 30, 2013 6:13 PM
    Moderator
  • Hello Cindy, I put the code in the VBA editor, selected the main table and I ran the macro, macros are enabled, but nothing happened? Can you guide me please, Thank you.
    Tuesday, October 1, 2013 2:46 AM
  • put the selection outside the table, where you want the result. the macro works on the first table in the document. or change the macro to work as required. my code is really only meant as an example, not afinal solution...

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 1, 2013 5:10 AM
    Moderator
  • Thanks Cindy, the nested tables should be in the first column, this works fine.
    Tuesday, October 1, 2013 10:20 AM