unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj, Buttons, ComCtrls;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
StatusBar1: TStatusBar;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TsampleCode = class
function Connect(): boolean;
procedure Disconnect();
function CreateDocument(): boolean;
function CreateTextTable(oDoc: Variant; oCursor: Variant; sName:
String; iRow: Integer; iColumn: Integer): Variant;
// need many more procs and funcs here ....
private
{ Private declarations }
staroffice : variant;
document:variant;
public
{ Public declarations }
end;
var
Form1: TForm1;
sample:tsamplecode;
implementation
{$R *.dfm}
function TSampleCode.CreateTextTable(oDoc: Variant; oCursor: Variant;
sName: String; iRow: Integer; iColumn: Integer): Variant;
var
ret: Variant;
begin
ret := oDoc.createInstance('com.sun.star.text.TextTable');
ret.setName(sName);
ret.initialize(iRow, iColumn);
oDoc.Text.InsertTextContent(oCursor, ret, False);
CreateTextTable := ret;
end;
function TSampleCode.Connect(): boolean;
begin
if VarIsEmpty(StarOffice) then
StarOffice := CreateOleObject('com.sun.star.ServiceManager');
Connect := not (VarIsEmpty(StarOffice) or VarIsNull(StarOffice));
end;
procedure TSampleCode.Disconnect();
begin
StarOffice := Unassigned;
end;
function TSampleCode.CreateDocument(): boolean;
var
StarDesktop: Variant;
otext:variant;
ocursor:variant;
begin
StarDesktop := StarOffice.createInstance('com.sun.star.frame.Desktop');
Document := StarDesktop.LoadComponentFromURL(
'private:factory/swriter', '_blank', 0,
VarArrayCreate([0, - 1], varVariant));
CreateDocument := not (VarIsEmpty(Document) or VarIsNull(Document));
//insert text here ... does not work tough....
//otext:=Document.text;
// need a proc in tsamplecode which creates the textcursor .....
//ocursor:=otext.createtextcursor();
//otext.insertstring(ocursor,'MyText',false);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
StatusBar1.SimpleText := 'Connecting to StarOffice ...';
Sample := TSampleCode.Create();
if Sample.Connect() then
StatusBar1.Panels[0].text:='Creating new text document ...';
try
if Sample.CreateDocument() then
begin
// can do someting here
//Button4.Enabled := true;
// Button5.Enabled := true;
// Button6.Enabled := true;
end;
StatusBar1.panels[0].Text := 'Ready';
except
StatusBar1.panels[0].Text := 'Error';
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
StatusBar1.panels[0].Text := 'Disconnecting from StarOffice ...';
// also do not know how to close the opened document from within delphi
Sample.Disconnect(); // this disconnects the delphi program but leaves
the the openoffice program running.
StatusBar1.SimpleText := 'Closed';
close;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
var
oDoc, oCursor:variant;
begin
//insert some more text
// how to insert text ?
// how to add lines, change fonts etc
end;
end.