PDA

View Full Version : [SOLVED:] Possible to Select Only Nested Tables



T-Belle
04-16-2018, 07:30 AM
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?

macropod
04-16-2018, 04:40 PM
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.

T-Belle
04-17-2018, 07:25 AM
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.

macropod
04-17-2018, 04:03 PM
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

T-Belle
04-18-2018, 03:34 AM
Thank you, Paul. Unfortunately, this didn't work. I really appreciate your trying to help me, though.

macropod
04-18-2018, 03:52 AM
In what way didn't it work? Did you add your formatting code where I had the MsgBox?

T-Belle
04-18-2018, 04:44 AM
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.

macropod
04-18-2018, 05:42 AM
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.

T-Belle
04-18-2018, 08:17 AM
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

macropod
04-18-2018, 03:54 PM
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.

T-Belle
04-19-2018, 03:26 AM
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). :)