Ask Your Question
0

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

asked 2017-11-12 15:54:36 +0200

Muru gravatar image

updated 2017-11-12 15:57:41 +0200

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/questi...'. 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

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-11-13 21:12:19 +0200

Jim K gravatar image

updated 2017-11-13 21:15:34 +0200

Start with the WriterDemo example as described at https://stackoverflow.com/questions/4.... 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 https://www.openoffice.org/api/docs/c....

edit flag offensive delete link more

Comments

Hi Jim, The provided solution is working well... :) Thanks a lot

Muru gravatar imageMuru ( 2017-11-14 08:01:57 +0200 )edit

Glad to hear it was helpful.

Jim K gravatar imageJim K ( 2017-11-14 16:16:45 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-11-12 15:54:36 +0200

Seen: 172 times

Last updated: Nov 13 '17