Optimal column width for all tables in odt documents


I have existing odt files with several tables. I would like to apply optimal column width for all tables of each document. A solution working from the command line would be ideal.
I tried to apply optimal column width to one of the tables and then looked into the content.xml file but it seems that there is no “optimal width” tag. It seems that the function recalculate the column width and then change that value into the xml file.

I found a solution.

The following macro (found here : http://www.oooforum.org/forum/viewtopic.phtml?t=60539) does the job :

Sub OptimzeColumnWidth
  Dim s As String
  Dim i As Long
  Dim oTables
  Dim oTable
  Dim oCell
  oTables = ThisComponent.getTextTables()
  If NOT oTables.hasElements() Then Exit Sub
  For i = 0 To oTables.getCount() - 1
    oTable = oTables.getByIndex(i)
    oFrame = ThisComponent.CurrentController.Frame
    oDispHelper = createUnoService("com.sun.star.frame.DispatchHelper")
    oDispHelper.executeDispatch(oFrame, ".uno:SelectTable", "", 0, Array())
 	oDispHelper.executeDispatch(oFrame, ".uno:SetOptimalColumnWidth", "", 0, Array()) 
End Sub

If I save the macro in a library in “my macros”, I can run it from the command line like that:

libreoffice "macro:///FormatPandocOdt.Module1.OptimzeColumnWidth" myfile.odt

But I would like to have the macro saved in a library within the document (not in my macros but in myfile.odt in the macro hierarchy) and I had to do this to make it work :

libreoffice "macro://./Standard.FormatPandocOdt.OptimzeColumnWidth" myfile.odt

With my security options (middle), libroffice open and ask if I want to activate the macros within the document.

I don’t know if my experience setting up a macro on the toolbar would be any help here. Thanks for posting this solution to your question, though.

Thanks David. For the moment It is not useful because I want to do it from outside Libreoffice. But as my report creation workflow is still in progress this may become useful eventually. So I keep it in mind…