本文介绍了如何搜索和替换odt Open Office文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Delphi应用程序中,我目前正在使用office ole自动化对doc和docx word文档进行编程搜索和替换。有没有人在OpenOffice中执行相同的代码(对于doc,docs,odt)?

In my Delphi application I am currently do Search&Replace programmatically for doc and docx word documents using office ole automation. Does anyone has the code to do the same (for doc, docs, odt) in OpenOffice?

我还问了一个

推荐答案

您应该关注界面。这是例子。请注意,没有错误处理。我已经用LibreOffice作家测试了,它对我来说很好。

You should take a focus on XReplaceable interface. Here is the example. Please note, that there's no error handling. I've tested it with LibreOffice writer and it works fine for me.

uses
  ComObj;

procedure OpenOfficeReplace(const AFileURL: string; ASearch: string; const AReplace: string);
var
  StarOffice: Variant;
  StarDesktop: Variant;
  StarDocument: Variant;
  FileReplace: Variant;
  FileParams: Variant;
  FileProperty: Variant;

begin
  StarOffice := CreateOleObject('com.sun.star.ServiceManager');
  StarDesktop := StarOffice.CreateInstance('com.sun.star.frame.Desktop');

  FileParams := VarArrayCreate([0, 0], varVariant);
  FileProperty := StarOffice.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
  FileProperty.Name := 'Hidden';
  FileProperty.Value := False;
  FileParams[0] := FileProperty;

  StarDocument := StarDesktop.LoadComponentFromURL(AFileURL, '_blank', 0, FileParams);

  FileReplace := StarDocument.CreateReplaceDescriptor;
  FileReplace.SearchCaseSensitive := False;
  FileReplace.SetSearchString(ASearch);
  FileReplace.SetReplaceString(AReplace);

  StarDocument.ReplaceAll(FileReplace);

  ShowMessage('Replace has been finished');

  StarDocument.Close(True);
  StarDesktop.Terminate;
  StarOffice := Unassigned;
end;

并使用示例

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenOfficeReplace('file:///C:/File.odt', 'Search', 'Replace');
end;

这篇关于如何搜索和替换odt Open Office文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 09:06