
C#でOpenOfficeを操作するソースコードをいくつかご紹介します。
今回はプレゼンテーション用のImpressをC#で自動表示させる為のコードを紹介します。
※OpenOfficeのダウンロードはこちらからどうぞ。
事前準備
C#からOpenOfficeを操作するためには「uno」と呼ばれるCOM APIを使用する必要があります。
こちらから事前にThe Apache OpenOffice Software Development Kitを取得して、下記のdllを参照へ追加しておきましょう。
- cli_basetypes.dll
- cli_cppuhelper.dll
- cli_oootypes.dll
- cli_ure.dll
- cli_uretypes.dll
- cli_uno.dll
※「cli_uno.dll」はOpenOfficeのインストールフォルダ内に存在しています
ソースコード
ヘッダ部には下記のとおり名前空間を追加しておきましょう。
using unoidl.com.sun.star.uno; using unoidl.com.sun.star.lang; using unoidl.com.sun.star.frame; using unoidl.com.sun.star.beans; using unoidl.com.sun.star.presentation; using unoidl.com.sun.star.util;
プレゼンテーションの起動は下記のコードとなります。
// コンポーネントコンテキストの取得
XComponentContext xContext = uno.util.Bootstrap.bootstrap();
// サービスマネージャの取得
XMultiServiceFactory xMsf = (XMultiServiceFactory)xContext.getServiceManager();
// コンポーネントローダの取得
XComponentLoader xLoader = (unoidl.com.sun.star.frame.XComponentLoader)xMsf.createInstance("com.sun.star.frame.Desktop");
// Impressの起動 ※"strImpressfile"はファイル名のURI
XComponent component = xLoader.loadComponentFromURL(strImpressFile, "_blank", 0, new PropertyValue[0]);
// Presenationオブジェクト取得
XPresentationSupplier xSupplier = (XPresentationSupplier)component;
XPresentation xPresentation = xSupplier.getPresentation();
// プレゼンテーション開始
xPresentation.start();
プレゼンテーションの切り替えは下記のコードです。
// コントローラの取得 XPresentation2 xP2 = (XPresentation2)xPresentation; // プレゼンテーションの切り替え xP2.getController().gotoSlideIndex(pageNo); //※pageNo・・・スライド番号
プレゼンテーションの終了は下記のコードです。
// プレゼンテーションの終了 xPresentation.end();
まとめ
CALCやWRITERなどのソースコードは紹介されていますが、IMPRESSはなかなか見つからなかったので書いてみました。
参考になれば幸いです。

