LibreOffice Writer Macro to set table width

I can’t make heads nor tails out of the documentation for uno in relation to text tables and how to use if from the Macro Basic…

sub TableFix
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem What goes here to set table width to automatic?


rem move on to next table ready for retrigger (and/or other macros to format contents)
dispatcher.executeDispatch(document, ".uno:JumpToNextTable", "", 0, Array())

Noting that the record macro doesn’t capture anything in dialog boxes and the keybinds don’t include table relative mode…
I’m at a loss for how to set the table to 100% width in a macro, and need to be able to do so. (Literally several thousand to do)
I can probably figure out the rest of it if I can get how to access the table property for setting the width.

It also doesn’t help that the one example I’ve found in the docs for setting table width in the docs is for creating a table, not modifying an existing.

My goal functionality is click into table, key-trigger a macro (easily set using the tools→customize dialog), have the macro do the equivalent of table→properties tab 0 width = automatic, tab 1, deselect can break across pages/columns, enter, hop to next table (which as can be seen, I can already do.

Had a switch of hardware platform, and macro recording picks up some things on mac that mint didn’t.
It inserts the following when I use the popup menu in the right side tray.
rem dispatcher.executeDispatch(document, ".uno:TableAlignment", "", 0, Array())

When I remove the remark rem command, it does absolutely nothing.
I know I need to fill in the properties array:

dim Array(0) as new com.sun.star.beans.PropertyValue
Array(0).Name =
Array(0).Value =

I don’t know what goes there for .uno:TableAlignment, nor how many elements it’s supposed to have.

I’ve tried

dim Array(0) as new com.sun.star.beans.PropertyValue
Array(0).Name = "TableAlignment"
Array(0).Value = 0

to match what I found on the table at https://wiki.documentfoundation.org/Development/DispatchCommands

I’ve verified that it’s indeed in the Writer section. It lists:

Dispatch command: uno:TableAlignment
Description: (blank)
Group: Table
Arguments: TableLeftSpace (long), TableRightSpace (long)
Internal name (value): SID_ATTR_TABLE_ALIGNMENT (21760)
Mode: U

Writer Basic - retrieve TextTable from TextTableCursor (originally "4.1")

on a cosmetic note : For...Next Statement :innocent:

For Each oTabla  in ThisComponent.TextTables
  With oTabla
  ...
1 Like

Macro with API:

Sub changeWidthsInTextTable
	dim oDoc as object, oTable as object, oVCur as object, oRow as object, oCell as object, oTableCursor as object, aSeps()
	oDoc=ThisComponent
	rem insert table
	oTable=oDoc.createInstance("com.sun.star.text.TextTable")
	with oTable
		.initialize(3, 4) '3 rows, 4 columns
		.RelativeWidth=100
		.HoriOrient=0
	end with
	oVCur=oDoc.CurrentController.ViewCursor
	oDoc.Text.insertTextContent(oVCur.End, oTable, false)

	'1st change of cell-width -> OK
	oRow=oTable.Rows(0) '1st row
	aSeps=oRow.TableColumnSeparators
	aSeps(0).Position=500
	oRow.TableColumnSeparators=aSeps()
	
	'2nd change
	oRow=oTable.Rows(2) '3rd row	
	
	'aSeps=oRow.TableColumnSeparators
	
	aSeps(0).Position=1500
	oRow.TableColumnSeparators=aSeps()

	rem functional with one variable aSeps2() used more times
	dim a$, i%, aSeps2()
	const max=3
	for i=1 to max
		a=CInt(inputbox("Index of row - step " & i & "/" & max, "Change cell in 1st column to 80% width", i-1))
		oRow=oTable.Rows(a)
		aSeps2=oRow.TableColumnSeparators 'no error
		aSeps2(0).Position=aSeps2(0).Position*0.8 'set only 80% of width
		oRow.TableColumnSeparators=aSeps2()
	next i
End Sub

The macro is little bit complicated bacause there is bug 157053 – Writer: error for re-used variable when macro changes the witdh of cells in TextTable

Wrong kind of work, Kamil. Not answering my qyestion.
①You’re inserting a new table.
② you’re setting individual column widths.

Since the tables I’m reformatting ALREADY EXIST, create new table is worthless. Not even helpful, since that’s basically just a rewrite of the documentation which I’ve already noted I can’t grasp the nuances of.

Moreover, I can’t even create new table and copy/paste, since these tables have many merged cells, so creating a new and copying in will automatically break them and lose data. (I’ve tried that).

(In other words, it’s clear you didn’t read the last paragraph of my request.)

I’m sorry, my answer was in a hurry, unfortunately I haven’t a lot of time now. Many UNO commands need a parameters, and Macro Recorder doesn’t record the parameters for some UNO commands.
I don’t know the exact way you asked, I know to set Width of Table with the ColumnSeparators.
You can get inserted table like: oTable=oDoc.TextTables.getByIndex(0) or oTable=oDoc.TextTables.getByName("My Table").
Can you upload the example with your table, it will be better.

Legally, I can’t - it’s other people’s IP. THe file I’m working with (one of 5 files) has over 200 tables. It’s 50 pages long with 5-10 per page.

I can, however, upload a sample file…

TableSample.odt (24.1 KB)

Keep in mind: that’s 2 tables in the sample; I’m staring down hundreds.

With this macro you get all tables of a document centered, WIth = 100% and Not break across pages
The format of a table with combined cells is very dificult
(sorry my English)

Sub CenterTables

Dim otablas As Object, oTabla As Object, i as Integer
oTablas = ThisComponent.TextTables
For i=0 to oTablas.getcount -1
 oTabla = oTablas.getbyIndex(i)
 With oTabla
     .HoriOrient = 2 
     .RelativeWidth = 100
     .BreakType = 0
     .Split = False    
 End With
Next

End Sub
1 Like

While not quite how I was hoping to do it, but it does a huge bit of what I needed. Many thanks, bantoniof!