Calc Sheet Activate event?

Is there an event for sheet Activation in a Calc document?

I want to take some action in code when a sheet is activated.
Is there a listener? Is this possible?

I created an Calc Sheet Activation Event Example over on Live LibreOffice Python UNO Examples.

The example can be run in a Codespace (directy in browser) on GitHub.

Yes…

Contextual menu in tab sheet, select Sheet Events... and set your macro in: Activate Document

@elmau From the OQ, I understand he wants to intercept a sheet change event, not the document activation one.

@vib If I understand your question correctly, no there’s currently no such event as Sheet change that could be specified in the document events. You’ll have to set a listener for that.

Here’s how I do it, from some working code:

Option Explicit

'--------------------------------------------------------------------

Public gHandlingSheetChangeEvent As Boolean		'flag that avoids to fire the same event twice
Public goSheetChangeListener As Object			'the listener
Public gListenerFlag As Boolean					'flag the listener execution

'--------------------------------------------------------------------

Sub RegisterSheetChangeEventListener

	If Not gListenerFlag Then
	    goSheetChangeListener = CreateUnoListener("SheetChangeListener_", "com.sun.star.sheet.XActivationEventListener" )
	    ThisComponent.CurrentController.addActivationEventListener(goSheetChangeListener)
	    gListenerFlag = True
	    gHandlingSheetChangeEvent = False
    End If

End Sub 'RegisterMyActivationEventListener

Sub UnRegisterSheetChangeEventListener

	If gListenerFlag Then
	    ThisComponent.CurrentController.removeActivationEventListener(goSheetChangeListener)
	    gHandlingSheetChangeEvent = False
	    gListenerFlag = False
    End If

End Sub 'UnRegisterMyActivationEventListener

Sub SheetChangeListener_activeSpreadsheetChanged(ByRef pEvt As Object)
'Fires when the sheet was changed.
'Ensures the selected cell is the one after the last used.
'pEvt.ActiveSheet holds the new sheet object.

	Dim lo_Cell As Object

    If Not gHandlingSheetChangeEvent Then
	    gHandlingSheetChangeEvent = True

		'----- do your stuff here -----

	    gHandlingSheetChangeEvent = False
    End If

End Sub 'SheetChangeListener_activeSpreadsheetChanged

Sub SheetChangeListener_disposing()
	'nothing
End Sub 'SheetChangeListener_disposing

HTH

He says clearly: sheet Activation

The event is named misleadingly “Activate Document”. Simply check it out.

This is a similar approach I figured out for python.

I implemented XActivationEventListener and attached to CurrenControler of Document.
See Also: XActivationBroadcaster

Thanks

Everything about this event is interesting. :slightly_smiling_face:
In the list of sheet events its name is OnFocus, in the user interface Activate Document.
Here is an example of a macro to handle the event (see @elmau 's first answer):

Sub OnActivation()
  Msgbox "Sheet activation"
End Sub

Instead of an event struct, the macro gets the StarDesktop as argument.