我有以下功能,它应该返回我正在搜索的文本第一次出现的位置。 问题:
我做了什么:
以下是代码:
function FindTextWord(wordApp: TWordApplication; afindText: OleVariant; startIndex, endIndex: Integer; findEndOffsetFast: Boolean): Integer;
var
matchCase : OleVariant;
matchWholeWord : OleVariant;
matchWildcards : OleVariant;
matchSoundsLike : OleVariant;
matchAllWordForms: OleVariant;
fWd : OleVariant;
wrap : OleVariant;
format : OleVariant;
replaceWith : OleVariant;
replace : OleVariant;
myRange : Range;
startSearchOffset: OleVariant;
endSearchOffset : OleVariant;
begin
WordApp.Selection.Start := 0;
WordApp.Selection.End_ := 0;
result:=-1;
try
if (Assigned(WordApp)) then
begin
if (startIndex<1) then
begin
WordApp.ActiveDocument.GrammarChecked:=true;
WordApp.ActiveDocument.SpellingChecked:=true;
WordApp.ActiveDocument.ShowGrammaticalErrors:=false;
WordApp.ActiveDocument.ShowSpellingErrors:=false;
startSearchOffset:=WordApp.ActiveDocument.Content.Start;
end else
begin
startSearchOffset:=startIndex;
end;
if (endIndex<1) then
begin
if (findEndOffsetFast)or(startIndex<1) then
begin
endSearchOffset:=WordApp.ActiveDocument.Content.End_;
end else
begin
endSearchOffset:=startSearchOffset+1;
WordApp.Selection.Start:=startSearchOffset;
while (WordApp.Selection.Start=startSearchOffset)and(endSearchOffset<WordApp.ActiveDocument.Content.End_)and(endSearchOffset-startSearchOffset<4000) do
begin
endSearchOffset:=endSearchOffset+1;
WordApp.Selection.End_:=endSearchOffset;
end;
endSearchOffset:=endSearchOffset-1-Length(afindText);
end;
end else
begin
endSearchOffset:=endIndex;
end;
myRange:=WordApp.ActiveDocument.Range(startSearchOffset,endSearchOffset);
myRange.Find.ClearFormatting;
myRange.Start:=Integer(startSearchOffset);
myRange.End_:=Integer(endSearchOffset);
MatchCase := False;
MatchWholeWord := TRUE;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
fWd := True;
Wrap := wdFindStop;
Format := False;
ReplaceWith := '';
Replace := wdReplaceNone;
if MyRange.Find.Execute(aFindText,MatchCase,MatchWholeWord,
MatchWildcards,MatchSoundsLike,
MatchAllWordForms,fWd,
Wrap,Format,ReplaceWith,Replace)
then begin
if (myRange.Start>=startSearchOffset) then
begin
if (myRange.Find.Found) then
begin
result:=myRange.Start;
end;
end else
begin
result:=FindTextWord(wordApp,afindText,startIndex,endIndex,false);
end;
end;
end;
except
end;
end;
更新
问题不在于替换文本(我必须删除可能出现在word文档中的所有HTML标记,但有以下转折:如果我遇到格式化标记,例如b,i,u,s,strong,以及比如,我必须删除它们并相应地格式化文本)
答案 0 :(得分:0)
以下是一些可能有用的旧Delphi 7代码:
PROCEDURE TFrmBuildReport.WordGlobalReplace(Orig,Repl: String);
VAR VOrigText,vReplText,vReplWhat: OleVariant;
BEGIN
VOrigText := Orig;
VReplText := Repl;
vReplWhat := wdReplaceAll;
WAppl.ActiveDocument.Content.Find.ClearFormatting;
WAppl.ActiveDocument.Content.Find.Replacement.ClearFormatting;
WAppl.ActiveDocument.Content.Find.Execute(VOrigText,vE,vE,vE,vE,vE,vE,vE,vE,VReplText,vReplWhat);
END; { WordGlobalReplace }
使用vE:= EmptyParam;