I’m using LibreOffice 7.1.4 and I would like to test a code sample for LibreOffice 7.2… Ok it will be not retrocompatible but I’m astanished the error I encouterred is for that reason.
The code of the sample is " Monitoring Document Events" at URL : https://help.libreoffice.org/latest/fr/text/sbasic/python/python_document_events.html
Full code is below. I copy it into only one basic ConsoleLogger module…
REM controller.Events module
Option Explicit
Private _obj As Object ' controller.ConsoleLogger instance
Sub OnLoad(evt As com.sun.star.document.DocumentEvent) ' >> Open Document <<
_obj = New ConsoleLogger : _obj.Start(evt)
End Sub ' controller.OnLoad
' ----
REM controller.ConsoleLogger class module
Option Explicit
Option Compatible
Option ClassModule
' ADAPTER design pattern object to be instantiated in the "Open Document" event
Private Const UI_PROMPT = True
Private Const UI_NOPROMPT = False ' Set it to True to visualise documents events
' CONSTRUCTOR/DESTRUCTOR
Private Sub Class_Initialize()
End Sub ' controller.ConsoleLogger.Initialize
Private Sub Class_Terminate()
End Sub ' controller.ConsoleLogger.Terminate
' MEMBERS
Private _evtAdapter As Object ' com.sun.star.document.XDocumentEventBroadcaster
' PROPERTIES
Private Property Get FileName As String
''' System-dependent filename '''
Const _LIBRARY = "Tools" : With GlobalScope.BasicLibraries
If Not .IsLibraryLoaded(_LIBRARY) Then .LoadLibrary(_LIBRARY)
End With
Filename = Tools.Strings.FilenameOutofPath(ThisComponent.URL)
End Property ' controller.ConsoleLogger.Filename
' METHODS
Private Sub _documentEventOccured(evt As com.sun.star.document.DocumentEvent)
''' Monitor document events '''
Access2Base.Trace.TraceLog("DEBUG", _
evt.EventName &" in "& Filename(evt.Source.URL), _
UI_NOPROMPT)
Select Case evt.EventName
Case "OnUnload" : _Stop(evt)
End Select
End Sub ' controller.ConsoleLogger._documentEventOccured
Private Sub _disposing(evt As com.sun.star.lang.EventObject)
End Sub ' controller.ConsoleLogger.disposing
Public Sub Start(Optional evt As com.sun.star.document.DocumentEvent)
''' Initialise document events logging '''
Const _LIBRARY = "Access2Base" : With GlobalScope.BasicLibraries
If Not .IsLibraryLoaded(_LIBRARY) Then .LoadLibrary(_LIBRARY)
End With : Access2Base.Trace.TraceLevel("DEBUG")
Access2Base.Trace.TraceLog("INFO", _
IIf(IsMissing(evt),"",evt.EventName & "-") & "Document events are being logged", _
UI_PROMPT)
_evtAdapter = CreateUnoListener( "_", "com.sun.star.document.XDocumentEventListener" )
ThisComponent.addDocumentEventListener( _evtAdapter )
End Sub ' controller.ConsoleLogger.Start
Private Sub _Stop(Optional evt As com.sun.star.document.DocumentEvent)
''' Terminate document events logging '''
ThisComponent.removeDocumentEventListener( _evtAdapter )
Access2Base.Trace.TraceLog("INFO", _
IIf(IsMissing(evt),"",evt.EventName & "-") & "Document events have been logged", _
UI_PROMPT)
Access2Base.Trace.TraceConsole() ' Captured events dialog
End Sub ' controller.ConsoleLogger._Stop
' EVENTS
' Your code for handled events goes here
So the error raised is in proc OnLoad and message is “variable undefined”. I understand it is variable _obj it can’t find Private _obj As Object declaration on top … I tried with Global declaration but it’s same problem. Then I tried with local Dim declaration and it’s running for this step but listener doesn’t work… Do you think I really need to upgrade to LibreOffice 7.2 to test it ?
Thank you for your response …