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 2019 I have open with success a document with this code:

Imports uno.util

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

 public sub OpenDocument()
           Dim oDocProps As Object
            oSM = CreateObject("")
            oDesk = oSM.createInstance("")

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

            oFrame = oDoc.getCurrentController().getFrame()
            _StatoControllo.CosaCercareTraProcessi = oFrame.Title
            oDocProps = oDoc.getDocumentProperties()
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()

and more:

 Class MyListener
        Implements XContainerListener
        Sub disposing(oEvent As EventObject) Implements XEventListener.disposing
        End Sub
        Sub XContainerListener_elementInserted([Event] As ContainerEvent) Implements XContainerListener.elementInserted
        End Sub
        Sub XContainerListener_elementRemoved([Event] As ContainerEvent) Implements XContainerListener.elementRemoved
        End Sub
        Sub XContainerListener_elementReplaced([Event] As ContainerEvent) Implements XContainerListener.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, 

But if I run application i have this error:

‘Impossibile eseguire il cast di
oggetti COM di tipo
‘System.__ComObject’ in tipi di
L’operazione non è stata completata
perché la chiamata QueryInterface sul
componente COM per l’interfaccia con
non è riuscita a causa del seguente
errore: Interfaccia non supportata.
(Eccezione da HRESULT: 0x80004002

why is so extremly difficulty?

more link:

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

  Private Sub CreaDocOffice(tipoApp As EnuTipoApplicazione)

        Dim localContext As = uno.util.Bootstrap.bootstrap()
        Dim multiServiceFactory As = CType(localContext.getServiceManager(),
        Dim componentLoader As XComponentLoader = CType(multiServiceFactory.createInstance(""), 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("")
        'Dim componentLoader As XComponentLoader = oSM.createInstance("")

        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,
        Dim MyListEvent As New CloseListener()
        AddHandler MyListEvent.Notifica, AddressOf EventExitLibreOffice
        Dim xc As XCloseBroadcaster = CType(xComponent, XCloseBroadcaster)
        'xc.addCloseListener(New CloseListener())

    End Sub

this is event result:

 Public Sub EventExitLibreOffice(msg As String)
    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!!!