• Finding Nested Tables with VBA in Word

    Home » Forums » Developers, developers, developers » DevOps Lounge » Finding Nested Tables with VBA in Word

    Tags:

    Author
    Topic
    #2376456

    I need to add formatting to the last line of the most nested table in a table in Word. The following code will find a nested table, but I need to do more than that. I need to check each table for nested tables. If that table has a nested table, I need to select that table. If that table has a nested table, I need that one instead. Then I need to add paragraph formatting to the last line.

    The following is what I have so far, but it only looks for a level 1 nested table, not the most nested table in a table:

    For Each mainTable In ActiveDocument.Tables

    n = n + 1
    NumOfTbl = NumOfTbl + 1
    ‘ Debug.Print “—— Table ” & n & ” ——”

    ‘nested level 1
    ns = 0
    For Each nestedTable1 In mainTable.Tables
    nestedTable1.Rows.Last.Select
    GetAllTablesInRange
    ‘MsgBox Selection.Tables.
    Selection.Tables(1).Select
    Selection.Tables(1).Rows.Last.Select
    Selection.ParagraphFormat.SpaceAfter = 0
    ns = ns + 1
    NumOfTbl = NumOfTbl + 1
    Next nestedTable1
    Next mainTable

    Thoughts?

    Viewing 1 reply thread
    Author
    Replies
    • #2376508

      I think you should put another iteration inside already existing iteration.

      The main iteration searches all tables in document, inner iteration then goes deeper through nested table inside.


      For i=1 to 65535
      // go through all tables
      // …
      For j=1 to 65535
      // go deeper into table i and check for nested tables
      // …
      Next j;
      Next i;

      Just my initial idea, needs some adjustment, like ending the iteration if it reaches the end of a table with Exit For command.

      Dell Latitude 3420, Intel Core i7 @ 2.8 GHz, 16GB RAM, W10 22H2 Enterprise

      HAL3000, AMD Athlon 200GE @ 3,4 GHz, 8GB RAM, Fedora 29

      PRUSA i3 MK3S+

      • This reply was modified 3 years, 10 months ago by doriel. Reason: code formatting
      • This reply was modified 3 years, 10 months ago by doriel. Reason: code formatting
    • #2376628

      I am not sure how to do that, but let me give you the original full text of the macro I am “borrowing”:

      <hr />

      Sub CountTables()
      Dim mainTable As Word.Table
      Dim nestedTable1 As Word.Table
      Dim nestedTable2 As Word.Table
      Dim n As Long
      Dim ns As Long
      Dim ns2 As Long
      Dim NumOfTbl As Long

      For Each mainTable In ActiveDocument.Tables

      n = n + 1
      NumOfTbl = NumOfTbl + 1
      Debug.Print “—— Table ” & n & ” ——”

      ‘nested level 1
      ns = 0
      For Each nestedTable1 In mainTable.Tables
      nestedTable1.Select
      ns = ns + 1
      NumOfTbl = NumOfTbl + 1
      Debug.Print “Nested Table ” & n & “.” & ns

      ‘nested level 2
      ns2 = 0
      For Each nestedTable2 In nestedTable1.Tables
      nestedTable2.Select
      ns2 = ns2 + 1
      NumOfTbl = NumOfTbl + 1
      Debug.Print “Nested Table ” & n & “.” & ns & “.” & ns2
      Next nestedTable2

      Next nestedTable1
      Next mainTable

      Debug.Print vbNewLine & “Total number of tables: ” & NumOfTbl

      End Sub

      <hr />

      I need to take one set of actions if nestedTable2 exists on nestedTable2. However, I need to take other action on nestedTable1 if nestedTable2 does NOT exist. However, if nestedTable2 does not exist, the current syntax goes straight to Next nestedTable1. I don’t really need the “for each” because there will only be 1 nestedTable2 in my scenario. How can I simply check if nestedTable2 exists?

      Thanks!!!

      • #2377034

        In the code you posted above, there already is FOR cycle inside FOR cycle (three of them), see:

        Sub CountTables()
        Dim mainTable As Word.Table
        Dim nestedTable1 As Word.Table
        Dim nestedTable2 As Word.Table
        Dim n As Long
        Dim ns As Long
        Dim ns2 As Long
        Dim NumOfTbl As Long

        For Each mainTable In ActiveDocument.Tables

        n = n + 1
        NumOfTbl = NumOfTbl + 1
        Debug.Print “—— Table ” & n & ” ——”

        ‘nested level 1
        ns = 0
        For Each nestedTable1 In mainTable.Tables
        nestedTable1.Select
        ns = ns + 1
        NumOfTbl = NumOfTbl + 1
        Debug.Print “Nested Table ” & n & “.” & ns

        ‘nested level 2
        ns2 = 0
        For Each nestedTable2 In nestedTable1.Tables
        nestedTable2.Select
        ns2 = ns2 + 1
        NumOfTbl = NumOfTbl + 1
        Debug.Print “Nested Table ” & n & “.” & ns & “.” & ns2
        Next nestedTable2
        Next nestedTable1
        Next mainTable

        Debug.Print vbNewLine & “Total number of tables: ” & NumOfTbl

        End Sub

        You need to think about the logic of the macro now. When do you reach desired table? Its the last time that nested table was discovored by VBA, for example. So program can “remember” the last position of the table, and if no more is discovered, it apllies your rule. For exmaple when program jumps to another table (the NEXT command), I consider that moment would be appropriate to perform some action.

        I need to take one set of actions if nestedTable2 exists on nestedTable2.

        Im lost in woods, my friend. What do you mean by that sentence? Cant understand what are you trying to achieve.

        Maybe it would be more appropriate to create the macro from scratch, instead of borrowing code from someone else.

        You need to take action if condition is met and different action when condiotion is not met?
        Use the simple IF command.

        If condition_1 Then
        action_1
        Else
        different_action
        End If

        Hope this helps.

        Dell Latitude 3420, Intel Core i7 @ 2.8 GHz, 16GB RAM, W10 22H2 Enterprise

        HAL3000, AMD Athlon 200GE @ 3,4 GHz, 8GB RAM, Fedora 29

        PRUSA i3 MK3S+

        • #2377065

          Thanks so much for your response!! Sorry for my typo!! The thing is I need to only apply my code when I find the most nested table.

          1. Sometimes it will be the first nested table. If so, apply my code.

          2. If that nested table has another nested table, I need to ignore that 1st level nested table and apply my code to the 2nd level nested table.

           

          I feel like the code above is very close, but I could be wrong. Appreciate your help in getting this over the finish line!!

          Thanks!!

    Viewing 1 reply thread
    Reply To: Finding Nested Tables with VBA in Word

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: