Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 22 of 22

Thread: Copy the out-most (primary/root/parent) table

  1. #21
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Function fcnParentTable(Optional lngNestLevel As Long = 0) As Table
    Dim oTbl As Table
    Dim lngNest As Long
    Dim oRng As Range
    'Pass 1 to return patriarch table, Pass 0 to return immediate parent.
    'Pass numbers greater than 1 to return ancestors other than patriarch of deep nested tables.
      On Error Resume Next
      Set oTbl = Selection.Tables(1)
      oTbl.Select
      On Error GoTo 0
      If Not oTbl Is Nothing Then
         With oTbl
           lngNest = .NestingLevel
            Set oRng = .Range.Cells(.Range.Cells.Count).Range
            oRng.Collapse wdCollapseEnd
          End With
          If lngNestLevel = 0 Then
            If lngNest > 1 Then
              lngNestLevel = lngNest - 1
            Else
              lngNestLevel = 1
            End If
          End If
          Do While lngNest > lngNestLevel
            Do
              oRng.Move Unit:=wdCharacter, Count:=1
            Loop While oRng.InRange(oTbl.Range)
            Set oTbl = oRng.Tables(1)
            lngNest = oRng.Tables(1).NestingLevel
          Loop
          If lngNestLevel <= lngNest Then
             Set fcnParentTable = oRng.Tables(1)
          End If
      End If
      Set oTbl = Nothing
      Set oRng = Nothing
    lbl_Exit:
      Exit Function
    End Function
    Greg

    Visit my website: http://gregmaxey.com

  2. #22
    VBAX Regular
    Joined
    May 2012
    Posts
    22
    Location
    Thanks!

    With a bit of modifying (feel free to take it further), this seems to work:

    Option Explicit
    Sub Test()
        Dim oTbl As Table
        Dim lNL As Long
        lNL = 2
        Set oTbl = fcnParentTable(lNL)
        If Not oTbl Is Nothing Then oTbl.Select
    End Sub
    
    
    Function fcnParentTable(Optional ByVal lngNestLevel As Long = 0) As Table
        Dim oTbl As Table
        Dim lngNest As Long
        Dim oRng As Range
         'Pass 1 to return patriarch table, Pass 0 to return immediate parent.
         'Pass numbers greater than 1 to return ancestors other than patriarch of deep nested tables.
        On Error Resume Next
        Set oTbl = Selection.Tables(1)
        oTbl.Select
    
    
        If lngNestLevel = 0 Then
            Set fcnParentTable = oTbl
            Exit Function
        End If
    
        On Error GoTo 0
        If Not oTbl Is Nothing Then
            With oTbl
                lngNest = .NestingLevel
                Set oRng = .Range.Cells(.Range.Cells.Count).Range
                oRng.Collapse wdCollapseEnd
            End With
            
            If lngNestLevel = 0 Then
                If lngNest > 1 Then
                    lngNestLevel = lngNest - 1
                Else
                    lngNestLevel = 1
                End If
            End If
            
            Do While lngNest > lngNestLevel
                Do
                    oRng.Move Unit:=wdCharacter, Count:=1
                Loop While oRng.InRange(oTbl.Range)
                Set oTbl = oRng.Tables(1)
                lngNest = oRng.Tables(1).NestingLevel
            Loop
            If lngNestLevel <= lngNest Then
                Set fcnParentTable = oRng.Tables(1)
            End If
            Set oTbl = Nothing
            Set oRng = Nothing
        End If
    lbl_Exit:
        Exit Function
    End Function
    Last edited by KilpAr; 08-27-2017 at 10:44 AM.

Posting Permissions

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