Delphi 使用OLE在Word文档中查找不起作用
我有以下函数,它应该返回我正在搜索的文本第一次出现的位置。 问题是:Delphi 使用OLE在Word文档中查找不起作用,delphi,ms-word,ole,delphi-5,Delphi,Ms Word,Ole,Delphi 5,我有以下函数,它应该返回我正在搜索的文本第一次出现的位置。 问题是: 返回-1 当我将整个文档内容用作范围(或使用具有文档内容边界的范围)时抛出AV-s 我做了什么: 大量的谷歌搜索,以找到问题的根源,或替代此代码 MSDN挖掘 反复试验 代码如下: function FindTextWord(wordApp: TWordApplication; afindText: OleVariant; startIndex, endIndex: Integer; findEndOffsetFast
- 返回-1
- 当我将整个文档内容用作范围(或使用具有文档内容边界的范围)时抛出AV-s
- 大量的谷歌搜索,以找到问题的根源,或替代此代码
- MSDN挖掘
- 反复试验
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;
函数FindTextWord(wordApp:TWordApplication;afindText:OleVariant;startIndex,endIndex:Integer;findEndOffsetFast:Boolean):Integer;
变量
火柴盒:油变异体;
匹配词:油变异体;
匹配通配符:OleVariant;
相配声音:油变型;
matchAllWordForms:OleVariant;
fWd:油变异体;
包装:油变型;
格式:OLEVANT;
替换为:油变异体;
替换:油变型;
我的范围:范围;
startSearchOffset:OleVariant;
endSearchOffset:OleVariant;
开始
WordApp.Selection.Start:=0;
WordApp.Selection.End:=0;
结果:=-1;
尝试
如果(已分配(WordApp)),则
开始
如果(startIndex这里有一些旧的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;问题与“替换”文本无关,这是您的代码段所做的。它还缺少vE
+1 Ken的声明,问题不是替换文本(我必须删除word文档中可能出现的所有HTML标记,但有以下扭曲:如果遇到格式化标记,如b、i、u、s、strong等,我必须删除它们并相应地格式化文本)