Squirrelly replaceByName behavior for a document objects' events

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.

You can see in [1], how to use method replaceByName, with Python, if you want clear event, you need pass argument aEvent, like uno.Any empty.

This work fine for me.

uno.invoke(
    doc.Events, 'replaceByName',
    ('OnLoad', uno.Any('[]com.sun.star.beans.PropertyValue', ()))
)

But, I sorry, I not know how to used in Basic.

Best regards

P.D. In my test, with wrong argument, never crash my document.

[1] OfficeDocument.py (revision 2f7c3cce) - OpenGrok cross reference for /core/wizards/com/sun/star/wizards/document/OfficeDocument.py

ok, now I know how, in Basic :slight_smile:

Dim args(1) As New com.sun.star.beans.PropertyValue

doc = ThisComponent
doc.Events.replaceByName("OnLoad", args)

Thanks for that, Mauricio. Still odd that you can clear a sheet object event by passing an empty variant to replaceByName but not to a document object, and still curious why getEvents on a document object doesn’t seem to return an object.

Anyway, thanks for the tip!

@osxtra,

This is a document object - ThisComponent and is passing a empty argument. Have tested and works without problem.

Also, as you have been helped, please help others to know the question has been answered by clicking on the :heavy_check_mark: in upper left area of answer which satisfied the question.