I spent ages trying to figure out how to set the columns widths of all tables in an OpenOffice Writer document using an OpenOffice Basic macro.
I finally worked it out and here is my solution.
In order to understand my macro you need to understand how OpenOffice tables operate.
Column widths are determined by the position of "column separators". A 5 column table will have 4 separators. The separator position is relative to the start of the table and the separator widths add up to 10000. Why 10000? I don't know!
Example :
A 5 column table with equal spacing will have 4 separators with a width of 2000. The column separators will have positions 2000 4000 6000 8000. The total column width is 10000.
If the table is 10cm wide then each column will be 2cm. If the table is 20cm wide each column will be 4cm. The columns Separator values are the same. The width of the table determines what the 10000 actually means in the real world.
This macro takes your desired widths, calculates the total, calculates how this relates to 10000 and then calculates the position of each separator.
It ignores tables with the wrong number of columns.
Sub SetTableColWidths
' Declare variables for later use
Dim i as integer ' Count integer
Dim x as integer ' Count integer
Dim oTables ' Collection of tables
Dim oTable ' Individual table
Dim aWidth() as double ' Array of widths
Dim iColCount ' Number of columns
Dim dFactor as double ' Unit factor
Dim dWidthTotal as double ' Total desired width
' Get all table objects in document
oTables = ThisComponent.TextTables
' Loop through one table at a time
For i = 0 To oTables.getCount() - 1
' Set table AutoFormat if required
'oTables(i).autoFormat("Blue")
' Initialise width total
dWidthTotal = 0
' #########################################
' This is the only bit you need to modify
iColCount = 5 ' Specify number of columns
ReDim aWidth(iColCount-1)
' Specify desired with for each column
' These are relative so can be whatever
' units you like. Populate the array with values
' Make sure you define the correct number
' of columns!
aWidth(0)= 2.3
aWidth(1)= 2.0
aWidth(2)= 0.9
aWidth(3)= 2.9
aWidth(4)= 6.5
' #########################################
' Get column separators
oTblColSeps = oTables(i).TableColumnSeparators
' Check table has correct number of columns
If ubound(oTblColSeps)+2<>iColCount Then
msgbox "Table """ & oTables(i).getName() & _
""" has wrong number of columns"
Else
' Sum all desired widths
For x = lbound(aWidth) To ubound(aWidth)
dWidthTotal = dWidthTotal + aWidth(x)
Next
' Calculate conversion factor as Writer requires
' column separater widths total 10000
oFactor = 10000/dWidthTotal
' Calculate first separator position based on factor and
' desired width of first column
oTblColSeps(0).position = oFactor * aWidth(0)
' For the other separators
For x = lbound(aWidth)+1 To (ubound(aWidth)-1)
' Position based on position of previous separator plus
' current width
oTblColSeps(x).position = oTblColSeps(x-1).position _
+ (oFactor * aWidth(x))
Next
' Update the table with the new positions
oTables(i).TableColumnSeparators = oTblColSeps
End If
Next
End Sub