[Solved] Calc macro fails if cell is active

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
retain
Posts: 3
Joined: Thu Dec 28, 2023 1:05 pm

[Solved] Calc macro fails if cell is active

Post by retain »

I have the following macro:

Code: Select all

Sub move
	Dim oSheet as Object
	Dim oCell as Object
	oSheet = ThisComponent.CurrentController.getActiveSheet()
	oCell = oSheet.getCellRangeByName("A1")
	oCell.setValue(123)
End Sub
If I open up a new spreadsheet and run the macro the value 123 appears in cell A1 as I expected. If I then delete the value but leave the focus in A1 and run the macro again nothing happens. If I click on any other cell and run the macro it works again.

I would like an understanding of this behaviour as I think it might be useful for avoiding bugs when I come to write macros.
Last edited by retain on Fri Jan 05, 2024 12:39 am, edited 1 time in total.
7.3.7.2 / LibreOffice Community, Ubuntu 22.04
User avatar
Zizi64
Volunteer
Posts: 11364
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Calc macro fails if cell is active

Post by Zizi64 »

I have the following macro:
How you lanch the macro? By a custom menu item, by a Form control element (button or other), by a ToolBar Icon, or by some other way: Directly from Macro menu...?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
retain
Posts: 3
Joined: Thu Dec 28, 2023 1:05 pm

Re: Calc macro fails if cell is active

Post by retain »

I opened a new spreadsheet and then: Tools -> Macros -> Run Macro... -> select macro by name -> Run
7.3.7.2 / LibreOffice Community, Ubuntu 22.04
User avatar
Zizi64
Volunteer
Posts: 11364
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Calc macro fails if cell is active

Post by Zizi64 »

Code: Select all

Remove the "focus" (the "Edit mode") from the A1 somehow. You can not modify a cell value by you macro while it is in "Edit mode". Here is a tip:
REM  *****  BASIC  *****
Sub move
	Dim oSheet as Object
	Dim oCell as Object
	Dim oCtrl as Object
	
	oCtrl = ThisComponent.CurrentController
	oSheet = ThisComponent.CurrentController.getActiveSheet()
	oCtrl.Select(oCtrl.ActiveSheet.GetCellRangeByName("B1"))
	oCell = oSheet.getCellRangeByName("A1")
	oCell.setValue(123)
	oCtrl.Select(oCtrl.ActiveSheet.GetCellRangeByName("A1"))
End Sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Post Reply