//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <SysUtils.hpp>
#include <ComObj.hpp> // a inclure obligatoirement
#include <Variants.hpp> // utile dans certains cas
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant vOOoOpenOffice, vOOoDesktop, vOOoLoadParams, vOOoDocument;
try
{
vOOoOpenOffice = CreateOleObject("com.sun.star.ServiceManager");
}
catch (...)
{
// si on passe ici c'est qu'il y a une erreur
}
if (VarType(vOOoOpenOffice) == varDispatch)
vOOoDesktop = vOOoOpenOffice.OleFunction("createInstance", "com.sun.star.frame.Desktop");
int Bounds[2] = {0,-1};
vOOoLoadParams = VarArrayCreate(Bounds, 1, varVariant);
//ouverture fichier existant
// FilePath = "file://localhost/C:/essai.sxc" -- pour un fichier Calc
//ou
// FilePath = "file:///C:/essai.sxc" -- pour un fichier Calc
AnsiString FileName = "file:///C:/";
// FileName += ExtractFilePath(Application->ExeName);
FileName += "essaitxt.odt";
FileName = StringReplace(FileName,"<A href="file://\\","/",TReplaceFlags">\\","/",TReplaceFlags()<< rfReplaceAll << rfIgnoreCase);
Variant vFileName = StringToOleStr(FileName);
vOOoDocument = vOOoDesktop.OleFunction("LoadComponentFromURL",vFileName,"_blank",0,vOOoLoadParams);
Variant vOOoCursor, vOOoText;
// acquisition du document
vOOoText = vOOoDocument.OleFunction("GetText");
// on cree un curseur
vOOoCursor = vOOoText.OleFunction("CreateTextCursor");
// on modifie les proprietes du texte
vOOoCursor.OlePropertySet("CharColor", 255);
vOOoCursor.OlePropertySet("CharShadowed", True);
String aText = "essai";
// on insere le texte
vOOoText.OleFunction("InsertString", vOOoCursor, aText.c_str(), false);
// on insere un caractere saut de ligne retour chariot
vOOoText.OleFunction("InsertControlCharacter", vOOoCursor, 0, false);
}
//---------------------------------------------------------------------------
Partager