Setting Focus on a button without a mouse, inside a macro

My aim is to produce a CALC spreadsheet that can be used WITHOUT a mouse (or pointer or trackpad)
My sheets contain a few macros which I know I can ‘fire’ from the top menu but most of the macros are triggered by buttons

So when writing a macro - how do I set the focus on to a particular button from the macro itself. ( Yes I know that the keyboard command CTL+W sets the focus onto the button - but how do I set the focus onto a particular button from within the macro? )

Thank you for your interest in this problem


You can set focus through the controls View. Here is the code:

Sub SetFocus
    Dim oSheet             As Object
    Dim oController      As Object
    Dim oForm              As Object
    Dim oControl          As Object
    Dim oControlView  As Object
Rem Get sheet - Active or by index or by name
Rem    oSheet = ThisComponent.getSheets().getByIndex(0)
Rem    oSheet = ThisComponent.getSheets().getByName("YOUR_SHEET_NAME")
Rem Get Controller
    oController = ThisComponent.getCurrentController()
Rem Get the Form - Use internal form name
    oForm = oSheet.getDrawpage().getForms().getByName("YOUR_INTERNAL_FORM_NAME")
Rem Get the control - Use your control name
    oControl = oForm.getByName("YOUR_CONTROL_NAME")
Rem Get the view of the control
    oControlView = oController.getControl(oControl)
Rem Set focus to the specified control
End Sub

There are different ways to access your sheet. Left all in (two Rem’ed) for your choice. Remember to change code to reflect your internal form name and control name also.

This is a perfect answer - thank you so much.

Hi. How can this be modified to work in any general sheet? removing the specific name into the Sub?


Don’t understand what “specific name into the Sub” means. Are you talking of the Sub name, sheet name, form name control name? Maybe a little more info on what you actually are attempting.