Error running macro sample "Listening to document event"

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 …

Please narrow your question. The link refers to a python-uno script but your code is in Basic. Also ultimately the question seems to be about a syntax error on a single line, yet you give a very large code sample. Even the line where the error occurs contains : to combine lines, which is counterproductive for determining what is causing the error.

Please also add tags to clarify the topic. Is this about Python? Basic? ConsoleLogger? Refine your code so that it is as small as possible but still shows the error and can easily be reproduced. At that point, edit the question to show the new code. If (as seems likely) you find the solution by yourself during this debugging process, then post your own solution and mark it as such.

1 Like

The link point to the sample code. First part at top of the page is python code, and after it is basic … There is only one line in proc OnLoad, it’s line number 6. The error is “variable undefined”. This code is published a an exemple for LibreOffice 7.2, I would like to understand why it doesn’t run with my station using LibreOffice 7.1.4 …

Sorry but this evening I can’t do better …

This is posted as an answer. Instead, you should have replied in a comment.

The help page example is partly incorrect, try this instead:

controller.Events module

Option Explicit
Global _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

Sub _documentEventOccured(evt As com.sun.star.document.DocumentEvent)
	''' ConsoleLogger unique entry point '''
    _obj.DocumentEventOccurs(evt)
End Sub ' controller._documentEventOccured

Sub OnUnload(evt As com.sun.star.document.DocumentEvent) ' >> Document closed <<
    _obj = Nothing
End Sub ' controller.OnUnload

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
    Private _txtMsg As String ' text message to log in console

    ' 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 DocumentEventOccurs(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.DocumentEventOccurs

    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)
        ''' Initialize document events logging '''
        Const _LIBRARY = "Access2Base" : With GlobalScope.BasicLibraries
            If Not .IsLibraryLoaded(_LIBRARY) Then .LoadLibrary(_LIBRARY)
        End With : Access2Base.Trace.TraceLevel("DEBUG")
        If IsMissing(evt) Then _txtMsg = "" Else _txtMsg = evt.EventName & "-"
        Access2Base.Trace.TraceLog("INFO", _txtMsg & "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 )
        If IsMissing(evt) Then _txtMsg = "" Else _txtMsg = evt.EventName & "-"
        Access2Base.Trace.TraceLog("INFO", _txtMsg & "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

It sounds like you were able to find the problem. Could you summarize the changes? The only difference I noticed was changing Private to Global when declaring _obj, but I did not go through all of it carefully.

Hi JimK

in controller.ConsoleLogger class module:

  • Misused IIF() statements raised the encountered error message.
  • empty routines can be removed
  • _documentEventOccured method required to be Public for the broadcaster to fire it. I declared it in …
    … controller.Events module:
  • declaring _obj as Global is a design choice.

The process can equally be coded procedurally, thus requiring only the controller.Events module

I patched the help accordingly, this will be available from release 7.3
https://gerrit.libreoffice.org/c/help/+/121456

Monitoring Document Events.odt (45.8 KB)
Monitoring Document Events.ods (17.1 KB)

I could not find a way, on my tablet, to attach an example file. Any idea?

The bottom right corner of the screenshot shows the icon with arrow-up that does the trick for me on the mobile.

Thx Mike K.