2

OOoForum.orgでのディスカッション

Pythonでは、pyunoを使用して、次のように実行できます。

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

これはC#では機能しないようです。これが私のテストコードです(私はおそらくステートメントを使用するすべてのものを必要としないことを理解していますが、私は他の誰かのコードを適応させています):

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);

        }
    }
}

ほとんどは正常に機能しているようですが、この行に到達すると、次のようになります。

table.initialize(2, 2);

ランタイムエラーが発生します:

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

どうやら、この行:

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

実際にはテーブルを何にも設定しません。

ここで何が起こっているのですか?

4

1 に答える 1

2

解決策 ( OOoForum.orgから):

テキスト テーブルは、サービス マネージャーのマルチサービス ファクトリからではなく、ドキュメント マルチサービス ファクトリから取得する必要があります。これを行うには、ドキュメント (モデル) を XMultiServiceFactory にキャストし、その createInstance メソッドを呼び出します。

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

DevGuideを参照してください。

于 2009-10-18T18:01:43.303 に答える