function Word_StringReplace(ADocument: TFileName; Ss, Rs:
string; Flags: TWordReplaceFlags): Boolean;
const
wdFindContinue = 1;
wdReplaceOne = 1;
wdReplaceAll = 2;
wdDoNotSaveChanges = 0;
var
WordApp: OLEVariant;
begin
Result := False;
{ Check if file exists }
if not FileExists(ADocument)
then
begin
ShowMessage('
Specified Document not found.');
Exit;
end;
{ Create the OLE Object }
try
WordApp := CreateOLEObject('
Word.Application');
except
on E:
Exception do
begin
E.
Message := '
Word is not available.';
raise;
end;
end;
try
{ Hide Word }
WordApp.Visible := False;
{ Open the document }
WordApp.Documents.Open(ADocument);
{ Initialize parameters}
WordApp.Selection.Find.ClearFormatting;
WordApp.Selection.Find.Text := Ss;
WordApp.Selection.Find.Replacement.Text := Rs;
WordApp.Selection.Find.
Forward := True;
WordApp.Selection.Find.Wrap := wdFindContinue;
WordApp.Selection.Find.Format := True;
WordApp.Selection.Find.MatchCase := wrfMatchCase
in Flags;
WordApp.Selection.Find.MatchWholeWord := True;
WordApp.Selection.Find.MatchWildcards := wrfMatchWildcards
in Flags;
WordApp.Selection.Find.MatchSoundsLike := False;
WordApp.Selection.Find.MatchAllWordForms := False;