Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.tables
|
|||
|
|||
For the archive, code to format (styles and column widths) tables and nested tables
All,
Being a heavy user of these newsgroups, I try to post code that I have found useful. The code below is based on code originally posted by Shauna Kelly. The code could be cleaned up quite a bit, but it works and hopefully it will prove helpful to someone. The first procedure calls the second procedure recursively. The second produre takes in a table object and an option parameter for width. The optional parameter is used when table object is a nested table and it represents the width of the cell containing the nested table. 'begin code Sub SetColumnWidthsOfAllTables() ' Based on code ' Posted on microsoft.public.word.tables newsgroup ' by Shauna Kelly 11/10/2002. Dim oTable As Table For Each oTable In ActiveDocument.Tables 'set style for entire table oTable.Range.Style = ActiveDocument.Styles("Table Body Text") 'don't allow rows to break across pages in any table oTable.Rows.AllowBreakAcrossPages = False 'call to sub SetColumnWidths oTable 'if the table contains nested table(1) If oTable.Tables.Count 0 Then 'loop over cells to find table 'do this because we want to be able to set cell's padding For Each acell In oTable.Range.Cells If acell.Tables.Count 0 Then acell.BottomPadding = InchesToPoints(0.1) acell.TopPadding = InchesToPoints(0.1) acell.RightPadding = InchesToPoints(0.1) 'call to sub, passing in the cell width SetColumnWidths acell.Tables(1), PointsToInches(acell.Width) End If Next acell End If Next oTable End Sub Sub SetColumnWidths(tbl As Object, Optional w As Single) ' Based on code ' Posted on microsoft.public.word.tables newsgroup ' by Shauna Kelly 11/10/2002. Dim textWidth As Single tbl.Borders.Enable = True tbl.AutoFitBehavior wdAutoFitFixed tbl.Select tbl.Rows(1).Shading.BackgroundPatternColor = wdColorGray125 'this is the width of the text on the page, in inches textWidth = PointsToInches(ActiveDocument.PageSetup.PageWidth - (ActiveDocument.PageSetup.LeftMargin + ActiveDocument.PageSetup.RightMargin)) If tbl.NestingLevel = 1 Then tbl.Rows.SetLeftIndent LeftIndent:=InchesToPoints(0.5), RulerStyle:=wdAdjustProportional textWidth = textWidth - 0.5 Else tbl.Rows.SetLeftIndent LeftIndent:=InchesToPoints(0.1), RulerStyle:=wdAdjustProportional End If If tbl.Columns.Count = 2 Then 'if this is a procedure table If InStr(LCase(tbl.Cell(1, 1).Range.Text), "step") Then tbl.Columns(1).Select Selection.Style = ActiveDocument.Styles("Table Body Text Centered Bold") tbl.Rows(1).Range.Style = ActiveDocument.Styles("Table Heading") tbl.Columns(1).Width = InchesToPoints(0.1 * textWidth) tbl.Columns(2).Width = InchesToPoints(0.9 * textWidth) 'if this is a description table ElseIf InStr(LCase(tbl.Cell(1, 1).Range.Text), "name") Then tbl.Rows(1).Range.Style = ActiveDocument.Styles("Table Heading") tbl.Columns(1).Width = InchesToPoints(0.5 * textWidth) tbl.Columns(2).Width = InchesToPoints(0.5 * textWidth) 'if this is a nested table, use the width passed in to this sub ElseIf tbl.NestingLevel = 2 Then tbl.Rows(1).Range.Style = ActiveDocument.Styles("Table Heading") tbl.Columns(1).Width = InchesToPoints(w * 0.25) tbl.Columns(2).Width = InchesToPoints(w * 0.75) 'default Else tbl.Rows(1).Range.Style = ActiveDocument.Styles("Table Heading") tbl.Columns(1).Width = InchesToPoints(0.25 * textWidth) tbl.Columns(2).Width = InchesToPoints(0.75 * textWidth) End If ElseIf tbl.Columns.Count = 3 Then If InStr(LCase(tbl.Cell(1, 2).Range.Text), "datatype") Then tbl.Columns(1).Width = InchesToPoints(1.5) tbl.Columns(2).Width = InchesToPoints(1.25) tbl.Columns(3).Width = InchesToPoints(2.75) Else tbl.Columns(1).Width = InchesToPoints(1) tbl.Columns(2).Width = InchesToPoints(1) tbl.Columns(3).Width = InchesToPoints(3.5) End If End If End Sub 'end code Kathryn |