1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| unit UnitOODoc;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
System.Win.ComObj, System.UITypes,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm16 = class(TForm)
Memo1: TMemo;
FileOpenDialog1: TFileOpenDialog;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form16: TForm16;
implementation
{$R *.dfm}
procedure TForm16.Button1Click(Sender: TObject);
var OO_FileName : String;
OO_Doc,OO_Service,OO_DeskTop,properties,CoreServ,prop : Variant;
Texte : Variant;
function FileToURL(F : String) : AnsiString;
var i : Word;
R : AnsiString;
begin
R:='file:///'+F;
// transforme le nom du fichier en adresse URL
while (Pos('\',R)>0) do {les \ en /}
begin
i:=Pos('\',R);
Delete(R,i,1);
Insert('/',R,i);
end;
while (Pos(' ',R)>0) do {les espaces en %20}
begin
i:=Pos(' ',R);
Delete(R,i,1);
Insert('%20',R,i);
end;
result:=R;
end;
begin
if FileOpenDialog1.Execute then
begin
if (VarIsEmpty(OO_Service) or VarIsNull(OO_Service)) then
OO_Service:=CreateOleObject('com.sun.star.ServiceManager');
if not(VarIsEmpty(OO_Service) or VarIsNull(OO_Service)) then
begin
if (VarIsEmpty(OO_DeskTop) or VarIsNull(OO_DeskTop)) then
OO_Desktop:= OO_Service.CreateInstance('com.sun.star.frame.Desktop');
end;
if (VarIsEmpty(OO_DeskTop) or VarIsNull(OO_DeskTop)) then
begin
MessageDlg('Le service Open Office n''est pas ouvert',mtError,[mbOk],0);
Exit;
end;
properties:=VarArrayCreate([0,0],varVariant);
CoreServ:= OO_Service.CreateInstance('com.sun.star.reflection.CoreReflection');
CoreServ.forName('com.sun.star.beans.PropertyValue').CreateObject(prop);
prop.name:='Hidden';
prop.Value:=True; //true
properties[0]:=prop;
// Ouverture document
OO_FileName:=FileToURL(FileOpenDialog1.FileName);
OO_Doc := OO_Desktop.LoadComponentFromURL(OO_FileName,
'_blank',
0,
properties);
texte:= OO_Doc.getText;
Memo1.Lines.Clear;
Memo1.Lines.Add(Texte.GetString);
OO_Service:= unAssigned;
end;
end;
end. |
Partager