[Solved] Saving to *.doc with Delphi/Pascal

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
florianp
Posts: 2
Joined: Thu Apr 24, 2014 5:12 pm

[Solved] Saving to *.doc with Delphi/Pascal

Post by florianp »

Hello,

I'm writing an Open Office link for my application, so our customers can create letters from templates and send them by mail/email.

I'm having some difficulties, however, to store the document in *.doc format (for MS WORD 97-XP).

I created a test project that takes text from a memobox and stores it in a new document. I can save the document as *odt without any problems. But when i give the parameters to save it as a *.doc, it creates a *.doc file, but the file is still in *.odt format (i.e. if i open it with MS Word, it gives an error message, asking permission to repair the file).

Could somebody pls give me an indication where I'm doing something wrong?

Code: Select all

procedure TFMain.btnexportClick(Sender: TObject);
var
   Desktop, LoadParams, Document, TextCursor, FilterParams:Variant;
   test : widestring;
begin
  Server := CreateOleObject('com.sun.star.ServiceManager'); //Server is declared as a Variant
  Desktop := Server.CreateInstance('com.sun.star.frame.Desktop');
  LoadParams := VarArrayCreate([0, -1], varVariant);
  Document := Desktop.LoadComponentFromURL('private:factory/swriter','_blank',0,LoadParams);
  TextCursor := Document.Text.CreateTextCursor;
  test := memo1.lines.text;
  document.Text.insertString(TextCursor,test,0);

  FilterParams := VarArrayCreate([0,0], varVariant);
  FilterParams[0] := CreateProperty('FilterName', 'MS Word 97');
  document.StoreToURL('file:///C:/test.doc',FilterParams);
end;

function TFMain.CreateProperty(const AName: AnsiString; AValue: Variant): Variant;
begin
  Result := Server.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  Result.Name := AName;
  Result.Value := AValue;
end;     
Last edited by Hagar Delest on Fri Apr 25, 2014 10:42 pm, edited 2 times in total.
Reason: tagged [Solved].
OpenOffice 4.0.1 On Windows 7
florianp
Posts: 2
Joined: Thu Apr 24, 2014 5:12 pm

Re: Saving to *.doc with Delphi/Pascal

Post by florianp »

OK, i've found a solution.

2 things:
- I was using the SDK of AOO 4.0.1, i'm not sure if the filter names have changed (at least for MS Word) since the v3.x.x.
=>I uninstalled v4 and installed Open Office 3.4.1
- my function CreateProperty didn't quite function well, so i stopped using it and implemented the code directly in my procedure (still looking to fix this)

In Pascal it would give this:

Code: Select all

procedure TFMain.btnexportClick(Sender: TObject);
var
  test: WideString;
begin
  Server := CreateOleObject('com.sun.star.ServiceManager');
  Desktop := Server.CreateInstance('com.sun.star.frame.Desktop');
  LoadParams := VarArrayCreate([0, 0], varVariant);
  LoadParams[0] := Server.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  LoadParams[0].Name := 'Hidden';
  LoadParams[0].Value := True;

  Document := Desktop.LoadComponentFromURL('private:factory/swriter',
    '_blank', 0, LoadParams);
  TextCursor := Document.Text.CreateTextCursor;
  test := memo1.Lines.Text;
  document.Text.insertString(TextCursor, test, 0);

  FilterParams := VarArrayCreate([0, 0], varVariant);
  FilterParams[0] := Server.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  FilterParams[0].Name := 'FilterName';
  FilterParams[0].Value := 'MS Word 97';

  document.StoreAsURL('file:///C:/test.doc', FilterParams);
  try
    document.Close(False);
  finally

  end;

end; 
In Delphi:

Code: Select all

procedure TFMain.Button1Click(Sender: TObject);
var
  test: WideString;
begin
  Server := CreateOleObject('com.sun.star.ServiceManager');
  Desktop := Server.CreateInstance('com.sun.star.frame.Desktop');
  LoadParams := VarArrayCreate([0, 0], varVariant);
  Param := Server.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  Param.Name := 'Hidden';
  Param.Value := True;
  LoadParams[0] := Param;

  Document := Desktop.LoadComponentFromURL('private:factory/swriter', '_blank', 0, LoadParams);
  TextCursor := Document.Text.CreateTextCursor;
  test := Memo1.Lines.Text;
  test := test;
  Document.Text.insertString(TextCursor, test, 0);

  FilterParams := VarArrayCreate([0, 0], varVariant);

  Param := Server.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  Param.Name := 'FilterName';
  Param.Value := 'MS Word 97';
  FilterParams[0] := Param;

  Document.StoreAsURL('file:///C:/OOLink/test.doc', FilterParams);
  try
    Document.Close(False);
  finally

  end;

end;
problem solved :)
OpenOffice 4.0.1 On Windows 7
Post Reply