Event Listener in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Kinimod
Posts: 6
Joined: Sun May 03, 2009 5:07 pm

Event Listener in Writer

Post by Kinimod »

Hi guys,

I've found examples for XModifyListener for Calc. Unfortunately I can't just simply copy them and the changes I made don't seem to work.

In my document I have 2 user-defined fields. One is the invoice number and one is date. I want to take the invoice number, modify the string and fill the date field. I did that successfully. Now I want to listen for changes of the invoice number field and run the macro.

This is how far I got, unfortunately it doesn't work.

Code: Select all

REM  *****  BASIC  *****
Global oListener as Object
Global InvoiceNumberField as Object
Global InvoiceDateField as Object

Sub Main
InvoiceNumberField = thisComponent.getTextFieldMasters().getByName("com.sun.star.text.FieldMaster.User.Rechnungsnummer")
InvoiceDateField = thisComponent.getTextFieldMasters().getByName("com.sun.star.text.FieldMaster.User.Datum")

oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener")
InvoiceNumberField.Content.addModifyListener(oListener)
End Sub

Sub Modify_modified(oEv)
updateDate()
End Sub

Sub Modify_disposing(oEv)
End Sub

Sub RmvListener
InvoiceNumberField = thisComponent.getTextFieldMasters().getByName("com.sun.star.text.FieldMaster.User.Rechnungsnummer")
InvoiceNumberField.removeModifyListener(oListener)
End Sub

Sub updateDate
InvoiceNumberField = thisComponent.getTextFieldMasters().getByName("com.sun.star.text.FieldMaster.User.Rechnungsnummer")
InvoiceDateField = thisComponent.getTextFieldMasters().getByName("com.sun.star.text.FieldMaster.User.Datum")

InvoiceDateField.Value = DateSerial(Mid(InvoiceNumberField.Content, 3, 4), Mid(InvoiceNumberField.Content, 7, 2), Mid(InvoiceNumberField.Content, 9, 2))
ThisComponent.TextFields.refresh
End Sub
This is my first macro, I hope it's not a catastrophe. :)
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Event Listener in Writer

Post by FJCC »

User fields don't seem to have a method to add a Modify Listener. On inspected my oInvoice variable with the MRI object inspector I see these methods for adding Listeners

Code: Select all

(Name)                        (Arguments)                                         (Return Type)            (DeclaringClass)     (Exceptions)  
acquire                       ()                                                  void                     .uno.XInterface        
addEventListener              ( [in] .lang.XEventListener xListener )             void                     .lang.XComponent     RuntimeException  
addPropertyChangeListener     ( [in] string aPropertyName, [in] .beans.XPropertyChangeListener xListener )  void     .beans.XPropertySet    
addVetoableChangeListener     ( [in] string PropertyName, [in] .beans.XVetoableChangeListener aListener )  void     
So something like this should work

Code: Select all

Sub AddListener
oListener = createUNOListener("Change_", "com.sun.star.beans.XPropertyChangeListener")
oTextFieldMasters = ThisComponent.TextFieldMasters
oInvoice = oTextFieldMasters.getByName("com.sun.star.text.fieldmaster.User.FirstField")
oInvoice.addPropertyChangeListener("Value", oListener)
End Sub

Sub Change_propertyChange(oEv)
Print "HERE I AM"
End Sub
However, I can't get the code to do anything when I modify the field. There are no errors, just silence. I don't have time to look at this more at the moment.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Arigamor
Posts: 2
Joined: Tue Aug 02, 2016 12:49 am

Re: Event Listener in Writer

Post by Arigamor »

Thank you FJCC for the code. I've been trying to attach a com.sun.star.script.XScriptListener to a DateField code with no sucess, even using Villeroy examples and codes. But yours worked.

The only problem is the property your're listening to, "Value" is not the property to listen to. In DateField is "Date" and should be attached to the Control. And in the TextField maybe is another property different than Value, that's why it didnt do anything.

I got the property to listen to via MRI as "DataFieldProperty".
Well, just wanted to leave this here for anyother looking for a solution.
OpenOffice 4.1.1 Windows Vista and Windows 7
Post Reply