Bug 157053

Summary: Writer: error for re-used variable when macro changes the witdh of cells in TextTable
Product: LibreOffice Reporter: Kamil Landa <kamlan>
Component: WriterAssignee: Not Assigned <libreoffice-bugs>
Status: NEW ---    
Severity: trivial CC: andreas.heinisch, buzea.bogdan, raal, stephane.guillou
Priority: medium    
Version: Inherited From OOo   
Hardware: x86-64 (AMD64)   
OS: Windows (All)   
Whiteboard:
Crash report or crash signature: Regression By:
Bug Depends on:    
Bug Blocks: 107659    

Description Kamil Landa 2023-09-01 13:46:54 UTC
I tried to change the Widths of Cells in TextTable via macro, but it is not possible to use one variable repeatedly for it. 

1) open new Writer document
2) run macro, it shows error message: "Object not accessible. Invalid use of an object."



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
	
	'BUG
	'redim aSeps() 'also bug
	aSeps=oRow.TableColumnSeparators '!!! Error "Object not accessible. Invalid use of an object."
	
	aSeps(0).Position=1500
	oRow.TableColumnSeparators=aSeps()	
End Sub



It is not possible to use aSeps() again, even the redim is used -> uncomment line: redim aSeps()



Version: 7.6.1.1 (X86_64) / LibreOffice Community
Build ID: c7cda394c5de06de37d8109c310df89a4d4c3a98
CPU threads: 8; OS: Windows 10.0 Build 17763; UI render: Skia/Raster; VCL: win
Locale: cs-CZ (cs_CZ); UI: cs-CZ
Calc: CL threaded

Also in older version:

Verze: 5.4.7.2 (x64)
ID sestavení: c838ef25c16710f8838b1faec480ebba495259d0
Vlákna CPU: 2; OS: Windows 6.1; Vykreslování UI: výchozí;
Národní prostředí: cs-CZ (cs_CZ); Calc: CL
Comment 1 Stéphane Guillou (stragu) 2023-09-15 14:56:30 UTC
Reproduced in:

Version: 24.2.0.0.alpha0+ (X86_64) / LibreOffice Community
Build ID: b3fdd999f87312447d03915585812b3a5cd48141
CPU threads: 8; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-AU (en_AU.UTF-8); UI: en-US
Calc: threaded

Same in OOo 3.3.

Using a different variable name (e.g. "aSeps2") works.

However, I found that you don't need to reassign a second time with aSeps=oRow.TableColumnSeparators for what you want to achieve. Try this:


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()	
End Sub

Does that fix your issue?

Related: https://ask.libreoffice.org/t/object-not-accessible-invalid-use-of-an-object-after-executeupdate/31829
Comment 2 Kamil Landa 2023-09-15 16:34:08 UTC
It isn't fix of problem, but it is easy to bypass bug now. If there is used 2nd variable for oRow.TableColumnSeparators, then this 2nd variable is usable repeatedly without bug. 
So bypass is -> use one variable for initial settings (without repeated aSeps=oRow.TableColumnSeparators, only aSeps(X).Position=...), and 2nd variable for potential changes. 


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
Comment 3 QA Administrators 2023-09-16 03:22:14 UTC Comment hidden (obsolete)