How can I create an XTextTable in OpenOffice.org without using C #?

Discussion at OOoForum.org

In python, using pyuno, I can do it like this:

table = self.model.createInstance("com.sun.star.text.TextTable")

      

This doesn't work in C #. Here is my test code (I realize that I probably don't need all those using instructions, but I am adapting the other's code):

using System;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.document;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.util;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.beans;

namespace FromScratch
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            XComponentContext componentContext =
                uno.util.Bootstrap.bootstrap();
            XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory)
                componentContext.getServiceManager();
            XTextDocument document;
            XComponentLoader loader = (XComponentLoader)
                multiServiceFactory.createInstance
                    ("com.sun.star.frame.Desktop");
            document = (XTextDocument) loader.loadComponentFromURL
                ("private:factory/swriter", "_blank", 0,
                 new PropertyValue[0]);

            XText text = document.getText();
            XTextCursor cursor = text.createTextCursor();

            XTextTable table = (XTextTable)
                multiServiceFactory.createInstance
                    ("com.sun.star.text.TextTable");
            table.initialize(2, 2);
            text.insertTextContent(cursor, table, false);

        }
    }
}

      

Most of them seem to work fine, but when it gets to this line:

table.initialize(2, 2);

      

I am getting runtime error:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at FromScratch.MainClass.Main (System.String[] args) [0x00063] in /home/matthew/Desktop/OpenOfficeSample/FromScratch/Main.cs:37

      

Apparently this line:

XTextTable table = (XTextTable)
    multiServiceFactory.createInstance
    ("com.sun.star.text.TextTable");

      

doesn't actually set the table.

What's going on here?

+2


source to share


1 answer


Solution (from OOoForum.org ):

You should get the text table from the multiservice factory document, not from the multiservice factory of the service manager. You can do this by dropping your document (model) down to the XMultiServiceFactory and calling its createInstance method.



XTextTable table = (XTextTable) 
    ((XMultiServiceFactory)document).createInstance 
    ("com.sun.star.text.TextTable");

      

See DevGuide .

+2


source







All Articles