Results 1 to 3 of 3
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Unnest a table using VBA (tested in 2002)

    I find it very aggravating when vendors supply "Word documents" which really are HTML formatted documents in a DOC wrapper. Most annoying are the nested tables that make formatting cleanup such a big headache. So I wrote some code...

    I couldn't find a way to refer directly to the "table that contains this table" so I cheated a bit using ranges. If anyone has a faster/cleaner/better technique, please share. Otherwise, y'all can use this one.
    <pre>Function PopOuterTable(tbl As Word.Table) As Boolean
    If tbl.NestingLevel = 1 Then
    'Nothing to do, clean up objects and return FALSE
    If Not (tbl Is Nothing) Then Set tbl = Nothing
    PopOuterTable = False
    Exit Function
    End If
    Dim rngTemp As Range
    Set rngTemp = tbl.Range
    ' Expand rngTemp to the outermost table containing tbl
    Do While rngTemp.Tables(1).NestingLevel > 1
    ' Expand the range one character beyond the nested table
    rngTemp.MoveEnd wdCharacter, 1
    ' .Tables(1) now refers to the containing table; expand rngTemp
    ' to that entire table
    Set rngTemp = rngTemp.Tables(1).Range
    Loop
    ' Convert ONLY the outermost table's content to text
    rngTemp.Tables(1).ConvertToText Separator:=wdSeparateByParagraphs, _
    NestedTables:=False
    ' Clean up object variables and return TRUE
    If Not (rngTemp Is Nothing) Then Set rngTemp = Nothing
    If Not (tbl Is Nothing) Then Set tbl = Nothing
    PopOuterTable = True
    End Function</pre>

    I probably will be passing Selection.Tables(1) to this function, but actually, this part was such a head-scratcher that I haven't gotten very far with the rest of the project...

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Unnest a table using VBA (tested in 2002)

    Try this on for size:
    <pre>Function PopOuterTable2(tbl As Table) As Boolean
    Dim t As Table
    For Each t In tbl.Parent.Tables
    If tbl.Range.InRange(t.Range) And tbl.NestingLevel > 1 Then
    t.ConvertToText wdSeparateByParagraphs, False
    PopOuterTable2 = True
    Exit Function
    End If
    Next t
    PopOuterTable2 = False
    End Function
    </pre>

    Only top-level tables are included in the document's Tables collection.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Unnest a table using VBA (tested in 2002)

    .InRange? There's something I never knew about. <img src=/S/smile.gif border=0 alt=smile width=15 height=15> Even in my test document with 18 top-level tables, this works quite quickly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •