none
Convert Nested Tables RRS feed

  • Question

  • I receive tables that have nested, single cell tables in the columns (with numeric data). I am looking for a way to search through my document from table to table. Look for those single cell tables and convert just those single cell tables to text. Besides doing this manually.
    Wednesday, April 5, 2017 5:33 PM

Answers

  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim oTbl As Table, Rng As Range, oCell As Cell
    For Each oTbl In ActiveDocument.Tables
      With oTbl
        For Each oCell In .Range.Cells
          With oCell
            Set Rng = .Range
            With Rng
              .End = .End - 1
              If Len(.Text) - Len(Replace(.Text, Chr(7), "")) = 2 Then
                .Tables(1).ConvertToText: .Characters.Last.Delete
              End If
            End With
          End With
        Next
      End With
    Next
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, April 5, 2017 11:13 PM
    • Proposed as answer by Chenchen LiModerator Monday, April 10, 2017 2:06 AM
    • Marked as answer by Matt WD Thursday, April 27, 2017 7:54 PM
    Wednesday, April 5, 2017 11:13 PM

All replies

  • What code have you tried so far?


    Peter Jamieson

    Wednesday, April 5, 2017 9:18 PM
  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim oTbl As Table, Rng As Range, oCell As Cell
    For Each oTbl In ActiveDocument.Tables
      With oTbl
        For Each oCell In .Range.Cells
          With oCell
            Set Rng = .Range
            With Rng
              .End = .End - 1
              If Len(.Text) - Len(Replace(.Text, Chr(7), "")) = 2 Then
                .Tables(1).ConvertToText: .Characters.Last.Delete
              End If
            End With
          End With
        Next
      End With
    Next
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, April 5, 2017 11:13 PM
    • Proposed as answer by Chenchen LiModerator Monday, April 10, 2017 2:06 AM
    • Marked as answer by Matt WD Thursday, April 27, 2017 7:54 PM
    Wednesday, April 5, 2017 11:13 PM
  • Hello Matt,

    According to your description, I think what you want is exporting value from the single cell table to it's parent cell, right?

    You could check if the cell have an inner table via Tables.Count property, and check if it is a single cell table via Cells.Count property.

    Here is the example.

    Sub Test()
    Dim TB1, TB2 As Table
    Dim cel As Cell
    Application.ScreenUpdating = False
    For Each TB1 In ActiveDocument.Tables
    For Each cel In TB1.Range.Cells
    If cel.Tables.Count = 1 Then
       Set TB2 = cel.Tables(1)
       If TB2.Range.Cells.Count = 1 Then
        cel.Range.Text = TB2.Cell(1, 1).Range.Text
       End If
    End If
    Next cel
    Next TB1
    Application.ScreenUpdating = True
    End Sub
    

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 6, 2017 5:25 AM
    Moderator
  • Not that great with coding. Have tried recording a macro to break the table to text, remove garbage spacing and reset as table. Always came back together with a lot of rework needing done.
    Thursday, April 6, 2017 12:47 PM
  • Will try thanks you for the quick reply.
    Thursday, April 6, 2017 12:49 PM
  • Will test shortly, thanks for the quick reply.
    Thursday, April 6, 2017 12:49 PM
  • Worked perfect, Thank You very much.
    Thursday, April 6, 2017 4:22 PM
  • Hello Matt,

    I suggest you mark helpful post as answer to close this thread. Thanks for your understanding.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 10, 2017 2:07 AM
    Moderator