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

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

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

Post 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
Last edited by Hagar Delest on Tue Jan 23, 2024 11:02 pm, edited 1 time in total.
Reason: tagged solved.
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
FJCC
Moderator
Posts: 9283
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Writer: merge cells in a table using Basic

Post by FJCC »

There is a sketch here of how to merge cells in a writer table.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Writer: merge cells in a table using Basic

Post 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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Writer: merge cells in a table using Basic

Post by JeJe »

Adapting your code to FJCC's better solution

Code: Select all


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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
D.Bugger
Posts: 15
Joined: Sun Oct 22, 2023 2:10 pm
Location: France

Re: Writer: merge cells in a table using Basic

Post by D.Bugger »

Thanks all! It works with mergerange.
Apache OpenOffice 4.1.14, AOO4114m1(Build:9811) - Rev. a0d24fb625, 2023-02-08 19:47
LibreOffice Version: 7.5.7.1 (X86_64) / LibreOffice Community Build ID: 50(Build:1)
OS: Linux 6.2; UI render: default; VCL: gtk3
Post Reply