Formatting tables to match can be rather tedious, especially if the tables came from another unformatted document. If you

1. add the two macros shown below
2. place the cursor in a table
3. run the FormatTable macro

Then you will have properly formatted headers, cells, borders, and shading.

Perhaps there is an easier way to do this, but I was not aware of it...

Sub FormatTable(Optional oUseTable)
 Dim oTable
 Dim oCell
 Dim nRow As Long
 Dim nCol As Long

 If IsMissing(oUseTable) Then
   oTable = ThisComponent.CurrentController.getViewCursor().TextTable
 Else
   oTable = oUseTable
 End If
 If IsNull(oTable) OR IsEmpty(oTable) Then
   Print "FormatTable: No table specified"
   Exit Sub
 End If

 Dim v
 Dim x
 v = oTable.TableBorder
 x = v.TopLine
 x.OuterLineWidth = 2
 v.TopLine = x

 x = v.LeftLine
 x.OuterLineWidth = 2
 v.LeftLine = x

 x = v.RightLine
 x.OuterLineWidth = 2
 v.RightLine = x

 x = v.TopLine
 x.OuterLineWidth = 2
 v.TopLine = x

 x = v.VerticalLine
 x.OuterLineWidth = 2
 v.VerticalLine = x

 x = v.HorizontalLine
 x.OuterLineWidth = 0
 v.HorizontalLine = x

 x = v.BottomLine
 x.OuterLineWidth = 2
 v.BottomLine = x

 'v.Distance = 51

 oTable.TableBorder = v

 For nRow = 0 To oTable.getRows().getCount() - 1
   For nCol = 0 To oTable.getColumns().getCount() - 1
     oCell = oTable.getCellByPosition(nCol, nRow)
     If nRow = 0 Then
       oCell.BackColor = 128
       SetParStyle(oCell.getText(), "OOoTableHeader")
     Else
       SetParStyle(oCell.getText(), "OOoTableText")
       If nRow MOD 2 = 1 Then
         oCell.BackColor = -1
       Else
         REM color is (230, 230, 230)
         oCell.BackColor = 15132390
       End If
     End If
   Next
 Next
'  Inspect(oTable)
'  Inspect(oCell)
End Sub

Sub SetParStyle(oText, sParStyle As String)
 Dim oEnum
 Dim oPar
 oEnum = oText.createEnumeration()
 Do While oEnum.hasMoreElements()
   oPar = oEnum.nextElement()
   If opar.supportsService("com.sun.star.text.Paragraph") Then
     'oPar.ParaConditionalStyleName = sParStyle
     oPar.ParaStyleName = sParStyle
   End If
 Loop
End Sub


--
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.sxw
My Book: http://www.hentzenwerke.com/catalog/oome.htm
Info:  http://www.pitonyak.org/oo.php
See Also: http://documentation.openoffice.org/HOW_TO/index.html

Reply via email to