函数开始
function ReplaceWord(FRange: OleVariant; const oldStr,
newStr: WideString): OleVariant;
begin
try
FRange.Find.Text := oldStr;
except
end;
try
FRange.Find.Replacement.Text := newStr;
except
end;
try
FRange.Find.Forward := true;
except
end;
try
FRange.Find.Wrap := 1;
except
end; //'wdFindContinue := 1;
try
FRange.Find.Format := false;
except
end;
try
FRange.Find.MatchCase := false;
except
end;
try
FRange.Find.MatchWholeWord := false;
except
end;
try
FRange.Find.MatchByte := false;
except
end;
try
FRange.Find.MatchWildcards := false;
except
end;
try
FRange.Find.MatchSoundsLike := false;
except
end;
try
FRange.Find.MatchAllWordForms := false;
except
end;
Result := FRange.Find.Execute(Replace := 2); //'wdReplaceAll:=2;
end;
函数结束
调用
var
wordApp,doc1,FRange
levariant;
begin
wordApp=CreateOleObject('Word.Application');
wordApp.DisplayAlerts :=0;// wdAlertsNone = 0
wordApp.Visible :=true;
doc1:=wordApp.Documents.open(cellCwfxFile);
FRange:=doc1.Range();
ReplaceWord(FRange,'oldStr','newStr');
end;