views:

209

answers:

4

I'm struggling with the following:

The goal is to parametrize an automation server for openoffice and I'm programming in Delphi.

The piece of basic code I want to translate into Delphi code is:

Dim aProps(1) As New com.sun.star.beans.PropertyValue
aProps(0).Name = "FilterName"
aProps(0).Value = "Text - txt - csv (StarCalc)"
aProps(1).Name = "FilterOptions"
aProps(1).Value = sFilterOptions

My attempt in Delphi looks like

type TPrmRecord = packed Record  
                    Name : String;  
                    Value : String;  
                  End;

Var
  ooParams:Variant;
  MyData : TPrmRecord;
Begin
  ooParams:= VarArrayCreate([0, 1], varVariant);

  MyData.Name  := 'FilterName';
  MyData.Value := 'Text - txt - csv (StarCalc)';
  ooParams[0]  := MyData; 

  MyData.Name  := 'FilterOptions';
  MyData.Value := '59/44,34,ANSI,1,';
  ooParams[1]  := MyData;
End;

This is not working does anyone have a suggestion how to tackle this?

+1  A: 

It looks as though you're missing the creation of the COM class, which would be the equivalent of the New com.sun.star.beans.PropertyValue line in your code.

I suspect you need to import the type library into Delphi which would give you the objects, properties and methods you need to emulate the Basic behaviour.

_J_
A: 

Have a look at this Thread in the german Delphi-PRAXiS forums. There is a whole delphi Unit posted doing some OOo automation.

Sebastian P.R. Gingter
+3  A: 

Your TPrmRecord type is not what OO.org expects. You should not try to write your own types, but use those that OO.org exposes.

There is an LPGL-licensed toolbox for Delphi: Delphi OOo. In it you will find a unit OOoTools.pas, which exports a function CreateUnoStruct(). Use this and pass 'com.sun.star.beans.PropertyValue' as the name of the struct. You will get a Variant (or an array of those, depending on the other parameter value) back that you can use instead of TPrmRecord (something like the following, untested):

var
  Params: Variant;
begin
  Params := CreateUnoStruct('com.sun.star.beans.PropertyValue', 1);

  Params[0].Name  := 'FilterName';
  Params[0].Value := 'Text - txt - csv (StarCalc)';

  Params[1].Name  := 'FilterOptions';
  Params[1].Value := '59/44,34,ANSI,1,';
end;
mghie
Hello mghie,Thank you very much for this answer, could you please be more specific of how to use this function in my case?Thanks Ad
addelichtman
Hello mghie that did the trick!I use this to open a CSV file into openoffice calc.Thank you very much for your time! Kind regards Ad.
addelichtman
This is the result I added this in the OOo toolboxIt opens a CSV file into calc:function OpenCSVSheet(Filename : String) : Variant;var ooParams: Variant; vTest : Variant;Begin vTest:= OpenOffice.CreateInstance('com.sun.star.frame.Desktop'); ooParams := CreateUnoStruct('com.sun.star.beans.PropertyValue', 1); ooParams[0].Name := 'FilterName'; ooParams[0].Value := 'Text - txt - csv (StarCalc)'; ooParams[1].Name := 'FilterOptions'; ooParams[1].Value := '59/44,34,ANSI,1,'; result := vTest.LoadComponentFromURL(FileName2URL(FileName), '_blank', 0, ooParams);End;
addelichtman
To be complete this is the FileName2URL routine:function FileName2URL(FileName: string): string;begin result:= ''; if LowerCase(copy(FileName,1,8))<>'file:///' then result:= 'file:///'; result:= result + StringReplace(FileName, '\', '/', [rfReplaceAll, rfIgnoreCase]);end;
addelichtman
A: 

use

var
ooParams:array[0..1] of TPrmRecord;

delphi uses strict type-casting, so this is causing an assignment error.

Talvi Watia