[Solved] Set Focus to Cell using Macro without selecting

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ttt_1978
Posts: 36
Joined: Thu Dec 02, 2010 1:13 pm

[Solved] Set Focus to Cell using Macro without selecting

Post by ttt_1978 »

I have form in a sheet that has a button to clean all the fields.
After pressing the button, I need to change the focus to the first field.
This is my code:

Code: Select all

Sub CleanFields()

Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object

Doc = ThisComponent
Sheet = Doc.Sheets.GetByName("Plan1")
Cell = Sheet.getCellRangeByName("D11")

If Msgbox("Do you want to clean all the fields?", 4 + 32 + 256) = 6 Then
	Sheet.getCellRangeByName("D11").String = ""
	Sheet.getCellRangeByName("D13").String = ""
	Sheet.getCellRangeByName("D15").String = ""
	Sheet.getCellRangeByName("D17").String = ""
	Sheet.getCellRangeByName("D19").String = ""
	Sheet.getCellRangeByName("D21").String = ""
	Sheet.getCellRangeByName("D23").String = ""
	Sheet.getCellRangeByName("D25").String = ""
	ThisComponent.CurrentController.Select(Cell)
End If

End Sub
With this code, after cleaning the fields, the focus changes to the cell D11, but it selects it.
I don't want it to be selected. When the cell stays selected, the user can't change the focus to the next field pressing "TAB" or "ENTER".
How can I change the focus to the first field (Cell D11) without selecting it?

Thanks.
Last edited by ttt_1978 on Tue Jun 23, 2015 9:53 pm, edited 1 time in total.
Windows XP/BrOffice 3.2
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Set Focus to Cell using Macro without selecting

Post by Villeroy »

Code: Select all

Sub gotoCell(sh,row,col)
view = ThisComponent.getCurrentController()
emptyContainer = createUnoService("com.sun.star.sheet.SheetCellRanges")
cell = ThisComponent.Sheets(sh).getCellByPosition(col, row)
view.select(cell)
view.select(emtpyContainer)
End Sub
gotoCell(0,1,2) goes to C2 on first sheet
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Set Focus to Cell using Macro without selecting

Post by Zizi64 »

This code will not select the target cell, but the TAB is not work after running the subroutine. You need click somewhere (or make other activity) to get back the 'manual control' on the sheet.

Code: Select all

sub MyGotoCell
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 ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$D$12"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
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.
ttt_1978
Posts: 36
Joined: Thu Dec 02, 2010 1:13 pm

[SOLVED] Re: Set Focus to Cell using Macro without selecting

Post by ttt_1978 »

Thanks Zizi64. Your code works.
Windows XP/BrOffice 3.2
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [SOLVED] Set Focus to Cell using Macro without selecting

Post by Zizi64 »

code works.
Yes, I tried launch it with a button, and the focus was stayed on the button.
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.
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: [SOLVED] Set Focus to Cell using Macro without selecting

Post by B Marcelly »

Zizi64 wrote:I tried launch it with a button, and the focus was stayed on the button.
Take focus off button and onto cell
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [SOLVED] Set Focus to Cell using Macro without selecting

Post by Villeroy »

There is also a button property "Take focus on click" which can be turned off.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
wbiene
Posts: 1
Joined: Fri Feb 24, 2012 11:21 pm

Re: [SOLVED] Set Focus to Cell using Macro without selecting

Post by wbiene »

Villeroy wrote: Wed Jun 24, 2015 7:28 am There is also a button property "Take focus on click" which can be turned off.
Thank you, this has helped me a lot.
OpenOffice 3.0.0 on OpenSUSE 11.1
Post Reply