How to get the opened document?

Java, C++, C#, Delphi, ??? - Using the UNO bridges

How to get the opened document?

Postby koohoolinn » Tue Sep 15, 2015 10:29 am

I'm writing an add-on that opens a dialog and I need to access the currently opened text document but I don't know how get it.

I'm using the OpenOffice plug-in in NetBeans and I started from an Add-on project. It created a class that gives me a XComponentContext instance but I don't know how to use it to get a OfficeDocument instance of the current document.

I've been googling for some time and I can't find any example that uses an existing, opened, document. They all start from a new document or a document that is loaded first so they have an URL for it.

Can you help me? Or ath least point me in the right direction?
OpenOffice 3.4.1 on Windows 7
koohoolinn
 
Posts: 3
Joined: Tue Sep 15, 2015 10:03 am

Re: How to get the opened document?

Postby RoryOF » Tue Sep 15, 2015 10:33 am

Is it not in
https://wiki.openoffice.org/wiki/API/Samples/Java/Office/DocumentHandling

Surely
Getting a remote office component context

The com.sun.star.comp.helper.Bootstrap.bootstrap() method initializes UNO and returns the remote component context of a runing OpenOffice.org process.

com.sun.star.uno.XComponentContext xContext = null;
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();

does what you ned?
Apache OpenOffice 4.1.6 on Xubuntu 18.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29432
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: How to get the opened document?

Postby koohoolinn » Tue Sep 15, 2015 2:54 pm

I got a bit further but I have no idea if it's OK:
Code: Select all   Expand viewCollapse view
private OfficeDocument getDocument() {
    if (this.officeDocument == null) {
        try {
            // this causes the error
            XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager();

            Object oDesktop = xMultiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", this.xComponentContext);
            XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);

            String url = "private:factory/swriter";
            String targetFrameName = "_self";
            int searchFlags = FrameSearchFlag.SELF;
            PropertyValue[] propertyValues = new PropertyValue[1];
            propertyValues[0] = new PropertyValue();
            propertyValues[0].Name = "Hidden";
            propertyValues[0].Value = Boolean.TRUE;

            XComponent xComponent = xComponentLoader.loadComponentFromURL(url, targetFrameName, searchFlags, propertyValues);

            XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent);
            this.officeDocument = new OfficeDocument(xModel);
        } catch (com.sun.star.uno.Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    return this.officeDocument;
}


When I try to add the extension, I get following error:
(com.sun.star.depoyment.DeploymentDescription){{ Message = "Error during activation of: VaphAddOn.jar", Context = (com.sun.star.uno.XInterface) @6ce03e0 }, Cause = (any) {(com.sun.star.registry.CannotRegisterImplementationException){{ Message = "", Context = (com.sun.star.uno.XInterface) @0 }}}}


This line causes the error:
Code: Select all   Expand viewCollapse view
XMultiComponentFactory xMultiComponentFactory = this.xComponentContext.getServiceManager();

The weird thing is that it's code that's in a private method that isn't called anywhere yet. Still it fails. :?

Any ideas?
OpenOffice 3.4.1 on Windows 7
koohoolinn
 
Posts: 3
Joined: Tue Sep 15, 2015 10:03 am

Re: How to get the opened document?

Postby koohoolinn » Tue Sep 15, 2015 3:13 pm

Hmm, when I click 'OK' on the error dialog, the add-on seems to be activated anyway. The test messages I put in my code appear in the output.

CORRECTION: it seems an old version is installed somehow. Newer messages do not appear.
OpenOffice 3.4.1 on Windows 7
koohoolinn
 
Posts: 3
Joined: Tue Sep 15, 2015 10:03 am


Return to External Programs

Who is online

Users browsing this forum: No registered users and 3 guests