How to receive the close event after creating a document libreoffice from VB.Net/C#?

Sorry if this question is reported into more posts.
But I have difficulty managing more concept…

to VB.net 2019 I have open with success a document with this code:


Imports unoidl.com.sun.star.lang
Imports unoidl.com.sun.star.uno
Imports unoidl.com.sun.star.bridge
Imports uno.util
Imports unoidl.com.sun.star.beans
Imports unoidl.com.sun.star.util
Imports unoidl.com.sun.star.container

 Private oSM, oDesk, oDoc As Object
 Private oFrame As Object

 public sub OpenDocument()
           Dim oDocProps As Object
            oSM = CreateObject("com.sun.star.ServiceManager")
            oDesk = oSM.createInstance("com.sun.star.frame.Desktop")

            Dim arProps() As unoidl.com.sun.star.beans.PropertyValue = New unoidl.com.sun.star.beans.PropertyValue() {}
            Dim arg(0)           
            
            Dim url As String = "private:factory/swriter"
            oDoc = oDesk.loadComponentFromURL(url, "_blank", 0, arg)


            oFrame = oDoc.getCurrentController().getFrame()
            Console.WriteLine(oFrame.Title)
            _StatoControllo.CosaCercareTraProcessi = oFrame.Title
            oDocProps = oDoc.getDocumentProperties()
            Console.WriteLine(oDocProps.Title)
End sub  

Now I have read many posts for Intercept onClose event
in this link
he proposes this code:

Dim xContainer As XContainer =  REM # TODO: Get object you want to listen to
        Dim myListener As MyListener = New MyListener()
        xContainer.addContainerListener(myListener)

and more:

 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

the problem remain this row:

Dim xContainer As XContainer =  REM # TODO: Get object you want to listen to

if I insert oDoc or oFrame receive ever message error

if i insert

Dim xContainer As XContainer = DirectCast(oDoc, XDocumentEventBroadcaster)

i have this error: “XDocumentEventBroadcaster not defined”

VB suggests me to write

Dim xContainer As XContainer = DirectCast(oDoc, unoidl.com.sun.star.document.XDocumentEventBroadcaster) 

But if I run application i have this error:

System.InvalidCastException:
‘Impossibile eseguire il cast di
oggetti COM di tipo
‘System.__ComObject’ in tipi di
interfaccia
‘unoidl.com.sun.star.document.XDocumentEventBroadcaster’.
L’operazione non è stata completata
perché la chiamata QueryInterface sul
componente COM per l’interfaccia con
IID
‘{5E6E43A6-4127-392D-8186-CFABE7507E7A}’
non è riuscita a causa del seguente
errore: Interfaccia non supportata.
(Eccezione da HRESULT: 0x80004002
(E_NOINTERFACE)).’

why is so extremly difficulty?

more link:

https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=67742

Thanks for anyone who just thought of helping me, but I solved it.

  Private Sub CreaDocOffice(tipoApp As EnuTipoApplicazione)

        Dim localContext As unoidl.com.sun.star.uno.XComponentContext = uno.util.Bootstrap.bootstrap()
        Dim multiServiceFactory As unoidl.com.sun.star.lang.XMultiServiceFactory = CType(localContext.getServiceManager(), unoidl.com.sun.star.lang.XMultiServiceFactory)
        Dim componentLoader As XComponentLoader = CType(multiServiceFactory.createInstance("com.sun.star.frame.Desktop"), XComponentLoader)


        Dim url As String
        Select Case _StatoControllo.TipoApplicazione
            Case EnuTipoApplicazione.Excel
                url = "private:factory/scalc"
            Case EnuTipoApplicazione.Presentazione
                url = "private:factory/simpress"
            Case EnuTipoApplicazione.Word
                url = "private:factory/swriter"
            Case Else
                url = "private:factory/swriter"
        End Select

        'oSM = CreateObject("com.sun.star.ServiceManager")
        'Dim componentLoader As XComponentLoader = oSM.createInstance("com.sun.star.frame.Desktop")


        Dim args As PropertyValue() = New PropertyValue(0) {}
        args(0) = New PropertyValue()
        args(0).Name = "MacroExecutionMode"
        args(0).Value = New Any(MacroExecMode.ALWAYS_EXECUTE_NO_WARN)
        Dim xComponent As XComponent = componentLoader.loadComponentFromURL(url, "_blank", 0, args)
        Dim frame As XFrame = CType(xComponent, unoidl.com.sun.star.text.XTextDocument).getCurrentController().getFrame()
        Dim MyListEvent As New CloseListener()
        AddHandler MyListEvent.Notifica, AddressOf EventExitLibreOffice
        Dim xc As XCloseBroadcaster = CType(xComponent, XCloseBroadcaster)
        xc.addCloseListener(MyListEvent)
        'xc.addCloseListener(New CloseListener())


    End Sub

this is event result:

 Public Sub EventExitLibreOffice(msg As String)
        MsgBox(msg)
    End Sub

and for last this is class of Listener:

Public Class CloseListener
                   Implements XCloseListener
        Public Event Notifica(msg As String)
         
        Private Sub XCloseListener_queryClosing(Source As EventObject, GetsOwnership As Boolean) Implements XCloseListener.queryClosing
                         RaiseEvent Notifica("queryclosing")
        End Sub

        Private Sub XCloseListener_notifyClosing(Source As EventObject) Implements XCloseListener.notifyClosing
                          RaiseEvent Notifica("notifyClosing")
        End Sub

        Private Sub XEventListener_disposing(Source As EventObject) Implements XEventListener.disposing
                          RaiseEvent Notifica("disposing")
        End Sub
    End Class

Grazie ancora!!!