0

我有以下函数,它应该返回我正在搜索的文本第一次出现的位置。问题:

  • 返回 -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;

更新

问题不在于替换文本(我必须删除可能出现在 word 文档中的所有 HTML 标签,但有以下变化:如果我遇到格式化标签,例如 b、i、u、s、strong 等,我必须删除它们并相应地格式化文本)

4

1 回答 1

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;

于 2012-10-11T20:43:59.880 回答