blackbird_dream Newbie

Joined: 25 Jun 2006 Posts: 4
|
Posted: Wed Jul 12, 2006 8:52 am Post subject: Ooowriter+delphi: how to insert text after bookmarks |
|
|
The following example needs the OOoconstants unit implemented by Mr Marcelly (thanks to him).
It opens the file named "test.odt" which is a Ooowriter document that contains a table with two bookmarks positioned in different cells (of course this document must be created first). the bookmarks are named "ici" and "la".
The programs writes down "hello world" at "ici" bookmark and "I am here now" at "la" bookmark.
Here is the full code :
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Comobj ,StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
uses OOoconstants;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
OpenDesktop : Variant;
LoadParams : Variant;
NomFichier : AnsiString; OpenOffice : Variant;
Document,text,cursor,xtable,cellule,tableborder,HelpLine,marks,anchor: Variant;
begin
OpenOffice:=CreateOleObject('com.sun.star.ServiceManager');
OpenDesktop := OpenOffice.createInstance('com.sun.star.frame.Desktop');
NomFichier := 'file:///'+StringReplace(extractFilePath(application.exename)+'test.odt', '\', '/', [rfReplaceAll, rfIgnoreCase]);
Document:=OpenDesktop.LoadComponentFromURL( NomFichier, '_blank',0, VarArrayCreate([0, - 1], varVariant));
Marks := Document.getBookmarks;
Anchor := Marks.getByName('ici').getAnchor;
Text := Anchor.getText;
Cursor := Text.createTextCursorByRange(Anchor.getStart);
Text.insertString(Cursor, 'hello world', False);
Anchor := Marks.getByName('la').getAnchor;
Text := Anchor.getText;
Cursor := Text.createTextCursorByRange(Anchor.getStart);
Text.insertString(Cursor, 'I am here now', False);
end;
end. |
|