{*
Textelemente in OpenOffice ersetzen
http://stackoverflow.com/questions/7806041/how-to-search-and-replace-in-odt-open-office-document
@param AFileURL Dateiname (file:///)
@param ASearch Liste mit den zu ersetzenden Begriffen (Textkennern)
@param AReplace Liste mit den Ersatzbegriffen
@param theErrorStr ehlermeldung
@return False bei einem Fehler
}
function OpenOfficeReplace(
const AFileURL:
String; ASearch, AReplace: TStringList;
var theErrorStr:
String): Boolean;
var
StarOffice: Variant;
StarDesktop: Variant;
StarDocument: Variant;
FileReplace: Variant;
FileParams: Variant;
FileProperty: Variant;
EmptyParam: Variant;
SaveParams: OleVariant;
SaveProperty: OleVariant;
iReplace: Integer;
sSearch:
String;
sReplace:
String;
begin
LogInFile('
PraTexKo OpenOfficeReplace Datei: '+AFileURL);
Result:= False;
theErrorStr:= '
';
try
EmptyParam:= VarArrayCreate([0,-1],varVariant);
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 := True;
FileParams[0] := FileProperty;
StarDocument := StarDesktop.LoadComponentFromURL(AFileURL, '
_blank', 0, FileParams);
FileReplace := StarDocument.CreateReplaceDescriptor;
FileReplace.SearchCaseSensitive := False;
for iReplace:=0
to ASearch.Count-1
do
begin
sSearch := ASearch[iReplace];
sReplace:= AReplace[iReplace];
FileReplace.SetSearchString(sSearch);
FileReplace.SetReplaceString(sReplace);
StarDocument.ReplaceAll(FileReplace);
end;
SaveParams:= VarArrayCreate([0, 0], varVariant);
SaveProperty := StarOffice.Bridge_GetStruct('
com.sun.star.beans.PropertyValue');
SaveProperty.
Name:= '
FilterName';
SaveProperty.Value:= '
MS Word 97';
SaveParams[0]:= SaveProperty;
StarDocument.storeAsUrl(AFileURL, SaveParams);
StarDocument.Close(True);
StarDesktop.Terminate;
StarOffice := Unassigned;
Result:= True;
except
on E:
Exception do
begin
theErrorStr:= E.
message;
end;
end;
end { OpenOfficeReplace } ;