macroslibreofficeword-processor

LibreOffice 4.1 Writer: macro to adjust column widths in tables


I am working on some LibreOffice macros that work on tables, in particular to set the width and height of each column and row to 0.85 cm (0.335 in).

In MS Office, this is easy, just select the table and in the macro have:

Selection.Rows.Height = CentimetersToPoints(0.85)
Selection.Columns.PreferredWidth = CentimetersToPoints(0.85)

There isn't anything like this in LibreOffice 4.1. It appears each column/row must be adjusted individually. Two ways to do this:

  1. Iterate through all the columns/rows and adjust each column/row

  2. Adjust the first column/row to some carefully calculated wide width/height, then call Distribute Columns/Rows Evenly

Just to get an idea of the code, I tried using the macro recorder and went through Table | Table Properties and played around until the table looked okay, but most of what I did was not recorded in the macro.

Has anyone done something like this?


Solution

  • Here is as far as I could get:

    sub Testing
    
        dim tables as object
        dim table as object
        dim columns as object
        dim column as object
        dim index as integer
    
        tables = ThisComponent.TextTables
    
        if tables.Count > 0 then
    
            table = tables.getByIndex(0)
            columns = table.columns
            table.Width = 850 * columns.Count '850 == 0.85 cm
    
            for index = 0 to columns.Count - 1
                column = columns.getByIndex(index)
                'column is always NULL
                'column.Width = 850
            next
    
        end if
    
    end sub
    

    Major problems noted:

    1. No way to retrieve the actual table you want to modify via ThisComponent.CurrentSelection, so instead hardcoded to the table at index 0

    2. Any changes to the table don't seem to be reflected in the document, and no obvious way to re-render or refresh Seems to be working now! But still looking for a way to call the function to distribute columns evenly

    3. columns.getByIndex always returns NULL!, and there's no documentation on how to use the column enumeration class within Basic

    Based on this investigation, would advise against trying to do anything productive with LibreOffice 4.1 Writer Basic macros.