开发者

Basic code to Delphi

开发者 https://www.devze.com 2022-12-12 05:16 出处:网络
I\'m struggling with the following: The goal is to parametrize an 开发者_运维知识库automation server for openoffice and I\'m programming in Delphi.

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?


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;


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.


Here is straight Delphi code without using Delphi OOo:

uses comobj;

var
  OO_ServiceManager: OleVariant;
  FileParams: OleVariant;

begin
  OO_ServiceManager := CreateOleObject ('com.sun.star.ServiceManager');
  FileParams := VarArrayCreate([0, 1], varVariant);
  FileParams[0] := OO_ServiceManager.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  FileParams[0].Name := 'FilterName';
  FileParams[0].Value := 'Text - txt - csv (StarCalc)';
  FileParams[1] := OO_ServiceManager.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  FileParams[1].Name := 'FilterOptions';
  FileParams[1].Value := '59/44,34,ANSI,1,';
end;


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


use

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号