Page 1 of 1

[Solved] Writer: merge cells in a table using Basic

Posted: Tue Jan 23, 2024 3:39 pm
by D.Bugger
Hi,

I'm trying to merge some cells in a table in Writer programmatically. My current code (all variables are declared):

Code: Select all

	Set cell= anchor.Cell
	Set tbl= anchor.textTable
	col = Asc(Left$(cell.CellName,1)) - Asc("A")
	row = CInt(Mid$(cell.CellName, 2)) - 1
	rf1= Chr$(Asc("A")+ col) & (row+1) & ":" & Chr$(Asc("A")+ tbl.getColumns().getCount()-1) & (row+1)
	Set cr2= tbl.getCellRangeByName(rf1)
	Call cr2.merge(True) 
The anchor is obtained from a bookmark inside the table, and I want to merge cells starting from the anchored cell to the right end of the table. The code executes up to the last line, and then I get the error message: "Instance member MERGE does not exist".

How can I merge some cells in code?

TIA

Re: Writer: merge cells in a table using Basic

Posted: Tue Jan 23, 2024 6:15 pm
by FJCC
There is a sketch here of how to merge cells in a writer table.

Re: Writer: merge cells in a table using Basic

Posted: Tue Jan 23, 2024 6:19 pm
by JeJe
Record a macro

Code: Select all




sub mergecells
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 ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:MergeCells", "", 0, Array())



Then use

Code: Select all

thiscomponent.currentcontroller.select cr2
mergecells

Re: Writer: merge cells in a table using Basic

Posted: Tue Jan 23, 2024 6:24 pm
by JeJe
Adapting your code to FJCC's better solution

Code: Select all


with thiscomponent.currentcontroller
.select cr2
.selection.mergerange
end with


Re: Writer: merge cells in a table using Basic

Posted: Tue Jan 23, 2024 9:10 pm
by D.Bugger
Thanks all! It works with mergerange.