Consulting

Results 1 to 11 of 11

Thread: Possible to Select Only Nested Tables

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    14
    Location

    Question Possible to Select Only Nested Tables

    I have a large MS Word document with many, many tables. Each table contains at least one nested table. Is it possible to select all nested tables (and only the nested tables) via a macro? I need to format these tables separately from the main (or parent) tables. I am very new to VBA. I have a macro to select all tables. I've attempted to modify it to select the nested tables but have failed miserably for hours. Any suggestions?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It is possible to write a macro that will only apply to tables that are nested inside others but, unless you can give us an indication of which cell(s) the nested tables might be in, such a macro would have to test each & every cell, making it quite slow.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Apr 2018
    Posts
    14
    Location
    Thank you for getting back to me.

    Each parent table is one large cell (basically creating a border around a collection of nested tables). There is text between the nested tables. I have attached an image/example.
    Attached Images Attached Images

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try something based on:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table, Cll As Cell
    For Each Tbl In ActiveDocument.Tables
      For Each Cll In Tbl.Range.Cells
        If Cll.Tables.Count > 0 Then
          With Cll.Tables(1)
            MsgBox .Rows.Count & vbTab & .Columns.Count
          End With
        End If
      Next
    Next
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Apr 2018
    Posts
    14
    Location
    Thank you, Paul. Unfortunately, this didn't work. I really appreciate your trying to help me, though.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In what way didn't it work? Did you add your formatting code where I had the MsgBox?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    Apr 2018
    Posts
    14
    Location
    Hey there, again, Paul.

    Attached is an image of my formatting code. I am unsure of how to integrate this with your suggested code. At this point, I couldn't care less about all the table border code. I'd really like the header row to be shaded (RGB: 234, 234, 234) with centered text and bold 8 point font. The rest of the table with the same font (not bold), aligned top of cell and left vertical aligned. I believe all that is captured in my code. I just cannot, for the life of me, get it to format only nested tables.
    Attached Images Attached Images

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    How about posting the actual code, instead of just an image of it - I don't propose to re-type it...

    That said, it's also by no means apparent what table 'objTable' in your code refers to.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Regular
    Joined
    Apr 2018
    Posts
    14
    Location
    Dim objTable As Word.Table
    For Each objTable In ActiveDocument.Tables
      objTable.Range.Font.Name = "TimesNewRoman"
      objTable.Range.Font.Size = 10
      With objTable
        .RightPadding = 5 'measurement in points
        .LeftPadding = 5 'measurement in points
        .TopPadding = 0 'measurement in points
        .BottomPadding = 0 'measurement in points
        .Rows.SpaceBetweenColumns = CentimetersToPoints(0.2)
        .Rows.AllowBreakAcrossPages = False
        .Rows.Alignment = wdAlignRowCenter
        .Rows.HeightRule = wdRowHeightAuto
        .Shading.Texture = wdTextureSolid
        .Shading.ForegroundPatternColor = wdColorWhite
        .Borders.InsideLineStyle = wdLineStyleSingle
        .Borders.InsideLineWidth = wdLineWidth050pt
        .Borders.OutsideLineStyle = wdLineStyleNone
        .Borders.InsideColor = wdColorGray25
        .Range.Cells.VerticalAlignment = wdAlignVerticalTop
        .PreferredWidthType = wdPreferredWidthPercent
        .PreferredWidth = 100
        With .Rows(1)
          .Range.Font.Name = "TimesNewRoman"
          .Range.Font.Size = 10
          .Range.Font.Bold = True
          .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
          .Cells.VerticalAlignment = wdAlignVerticalTop
          .Shading.Texture = wdTexture10Percent
          .Shading.ForegroundPatternColorIndex = wdBlack
          .Shading.BackgroundPatternColorIndex = wdWhite
          .Borders.InsideLineStyle = wdLineStyleSingle
          .Borders.InsideLineWidth = wdLineWidth050pt
          .Borders.InsideColor = wdColorGray25
          .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
          .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
          .Borders(wdBorderTop).LineWidth = wdLineWidth150pt
          .Borders(wdBorderBottom).LineWidth = wdLineWidth150pt
          .Borders(wdBorderTop).Color = wdColorBlack
          .Borders(wdBorderBottom).Color = wdColorBlack
          .HeadingFormat = True
        End With 'end the first row settings
        With .Rows(.Rows.Count) 'the last row in the table
          .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
          .Borders(wdBorderBottom).LineWidth = wdLineWidth150pt
          .Borders(wdBorderBottom).Color = wdColorBlack
          .Borders.InsideLineStyle = wdLineStyleSingle
          .Borders.InsideLineWidth = wdLineWidth050pt
          .Borders.InsideColor = wdColorGray25
        End With 'end the last row settings
      End With 'end that table settings
    Next objTable
    Last edited by macropod; 04-18-2018 at 03:15 PM. Reason: Added code tags & formatting

  10. #10
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The code you've supplied applies to all tables in the document - nothing about it indicates that you've made any changes to accommodate it to the code I posted, so it only applies to the nested table. For example:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table, Cll As Cell
    For Each Tbl In ActiveDocument.Tables
      For Each Cll In Tbl.Range.Cells
        If Cll.Tables.Count > 0 Then
          With Cll.Tables(1) 'Nested Table as a whole
            .Range.Cells.VerticalAlignment = wdAlignVerticalTop
            .PreferredWidthType = wdPreferredWidthPercent
            .PreferredWidth = 100
            .RightPadding = 5 'measurement in points
            .LeftPadding = 5 'measurement in points
            .TopPadding = 0 'measurement in points
            .BottomPadding = 0 'measurement in points
            .Shading.Texture = wdTextureSolid
            .Shading.ForegroundPatternColor = wdColorWhite
            With .Range.Font
              .Name = "TimesNewRoman"
              .Size = 10
            End With
            With .Rows
              .SpaceBetweenColumns = CentimetersToPoints(0.2)
              .AllowBreakAcrossPages = False
              .Alignment = wdAlignRowCenter
              .HeightRule = wdRowHeightAuto
            End With
            With .Borders
              .InsideLineStyle = wdLineStyleSingle
              .InsideLineWidth = wdLineWidth050pt
              .InsideColor = wdColorGray25
              .OutsideLineStyle = wdLineStyleNone
            End With
            With .Rows(1) 'First Row variation
              .HeadingFormat = True
              With .Range
                .Font.Bold = True
                .ParagraphFormat.Alignment = wdAlignParagraphCenter
              End With
              With .Shading
                .Texture = wdTexture10Percent
                .ForegroundPatternColorIndex = wdBlack
                .BackgroundPatternColorIndex = wdWhite
              End With
              With .Borders
                .Item(wdBorderTop).LineStyle = wdLineStyleSingle
                .Item(wdBorderBottom).LineStyle = wdLineStyleSingle
                .Item(wdBorderTop).LineWidth = wdLineWidth150pt
                .Item(wdBorderBottom).LineWidth = wdLineWidth150pt
                .Item(wdBorderTop).Color = wdColorBlack
                .Item(wdBorderBottom).Color = wdColorBlack
              End With
            End With
            With .Rows(.Rows.Count) 'Last Row variation
              With .Borders
                .Item(wdBorderBottom).LineStyle = wdLineStyleSingle
                .Item(wdBorderBottom).LineWidth = wdLineWidth150pt
                .Item(wdBorderBottom).Color = wdColorBlack
              End With
            End With
          End With
        End If
      Next
    Next
    Application.ScreenUpdating = True
    End Sub
    Note: I've tidied up your code and added more structure and formatting to it.


    PS: When posting code, please format your code and use the code tags, indicated by the # button on the posting menu. Without them, your (formatted) code loses much of whatever structure it had.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  11. #11
    VBAX Regular
    Joined
    Apr 2018
    Posts
    14
    Location
    Thank you!!! It works. What a fantastic way to start my work day. You are my hero. You can't imagine how much time and effort this will save. I've spent as much as 10-12 hours formatting large documents with tables and nested tables. It is mind-numbing.

    Anyway, thank you! Thanks also for cleaning up my code. Clearly, I have a long way to go with learning VBA (and posting to this site).

Tags for this Thread

Posting Permissions

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