For a document object,
oEvents = ThisComponent.getEvents
does not seem to return an object (try running XRay on the above)
yet calling oEvents.elementNames returns a string array of all events associated with the object.
oEvents.getByName(“Onload”) returns either an empty variant (no routine assigned to the “OnLoad” event) or a two-element array of type com.sun.star.beans.PropertyValue (let’s call it aEvent):
aEvent(0):
- .Name = “Script”
- .Handle = 0
- .Value = “vnd.sun.star.script:Standard.Module1.YOUR_ROUTINE_HERE?language=Basic&location=document”
- .State = 0
aEvent(1):
- .Name = “EventType”
- .Handle = 0
- .Value = “Script”
- .State = 0
aEvent(0)'s .Value entry may be different if your module has another name, you’re using Python instead of Basic, etc.
For a sheet, it works more as expected.
oEvents = ThisComponent.getSheets.getByIndex(0).getEvents
Returns a “ScSheetEventsObj” object (as opposed to no apparent object).
oEvents.elementNames also gives you a list of the available events (a different list, of course.)
oEvents.getByName(“OnDoubleClick”)
also returns aEvent (again, either populated or empty), except if it’s populated the order of the elements is reversed, so aEvent(0) holds the “EventType” element, and (1) holds the “Script” element.
To set an event, make sure aEvent is populated, and call
oEvents.replacyByName(sEvent, aEvent)
where sEvent is a valid event name from the elementNames array.
Here’s where it gets squirrelly.
To remove an event, you’d think you could just pass an empty variant aEvent to oEvents.replaceByName(sEvent, aEvent)
For a sheet, that works fine.
For a document, it crashes replaceByName
Furthermore, for a document, if you try to get around the crash by passing an empty string for the .Value property of the “Script” element, replaceByName “loses” the element, and the only way to recover is either have a backup document, or create a new one and copy all your sheets, ranges, forms, etc. into it, updating formula references as needed, and so on.
Ugh.
Once broken, passing a “normal” two-element aEvent into replaceByName on a document object has no effect. It stays broken.
Does anyone know why document objects exhibit this strange behavior? Hate to call it a bug, but it sure doesn’t act like one would expect.