How to raise a OnLoad event for writer document of LibreOffice from VB.Net/C#?

I am searching some example VB.NET/C# code to raise OnLoad & OnSave events for LibreOffice Writer document.

So referring the below code from the link ‘https://ask.libreoffice.org/en/question/136735/how-do-we-import-the-listener-events-of-libreoffice-writer-in-visual-basic-6/#’. Here please anyone can tell how to get object and assign to variable ‘Dim xContainer As XContainer =’

Imports unoidl.com.sun.star.container

Imports unoidl.com.sun.star.lang

Module Module1 Sub Main()
    Dim oSM
    Dim oDesk, oDoc As Object
    Dim arg(-1)
    oSM = CreateObject("com.sun.star.ServiceManager")
    oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
    oDoc = oDesk.loadComponentFromURL("private:factory/swriter", "_blank", 0, arg)
    oDoc.getText().setString("Hello!")
    Dim xContainer As XContainer =  REM # TODO: Get object you want to listen to
    Dim myListener As MyListener = New MyListener()
    xContainer.addContainerListener(myListener)
End Sub
Class MyListener
    Implements XContainerListener
    Sub disposing(oEvent As EventObject) Implements XEventListener.disposing
        MsgBox("disposing")
    End Sub
    Sub XContainerListener_elementInserted([Event] As ContainerEvent) Implements XContainerListener.elementInserted
        MsgBox("elementInserted")
    End Sub
    Sub XContainerListener_elementRemoved([Event] As ContainerEvent) Implements XContainerListener.elementRemoved
        MsgBox("elementRemoved")
    End Sub
    Sub XContainerListener_elementReplaced([Event] As ContainerEvent) Implements XContainerListener.elementReplaced
        MsgBox("elementReplaced")
    End Sub
End Class

End Module

Start with the WriterDemo example as described at visual studio - VB.Net example for LibreOffice SDK - Stack Overflow. Add the following code at the end of the Main() subroutine.

    Dim myListener As MyListener = New MyListener()
    Dim docEventBroadcaster As XDocumentEventBroadcaster = DirectCast(
        xComponent, XDocumentEventBroadcaster)
    docEventBroadcaster.addDocumentEventListener(myListener)
    For waitForEvents = 1 To 100
        Thread.Sleep(1000)
        Console.WriteLine(waitForEvents)
    Next
End Sub

Class MyListener
    Implements XDocumentEventListener
    Public Sub disposing(Source As unoidl.com.sun.star.lang.EventObject) Implements unoidl.com.sun.star.lang.XEventListener.disposing
        MsgBox("disposing")
    End Sub

    Public Sub documentEventOccured(oEvent As DocumentEvent) Implements XDocumentEventListener.documentEventOccured
        MsgBox("documentEventOccured " & oEvent.EventName)
    End Sub
End Class

To test, run the code and save the document, which should cause the message box to be displayed showing the events that occurred.

API documentation is at Interface XDocumentEventListener.

Hi Jim, The provided solution is working well… :slight_smile:
Thanks a lot

Glad to hear it was helpful.