0

i have the following function, which should return the position of the first occurrence of the text i'm searching for. The problem:

  • returns -1
  • throws AV-s, when i'm using the whole document content as a range (or using a range with the boundaries of the document content)

What have i done:

  • Lots of Google searches, to find the source of the problem, or alternatives to this code
  • MSDN digging
  • Trial and error

Here is the code:

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;

Update

The problem is not replacing text (i must remove all HTML tags which may appear in the word document, but with the following twist: if i encounter formatting tags, such as b, i, u, s, strong, and the like, i have to remove them and format the text accordingly)

beerwin
  • 9,813
  • 6
  • 42
  • 57

1 Answers1

0

Here's some old Delphi 7 code that may help:

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 }

with vE := EmptyParam;

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
  • 1
    The question has nothing to do with "replacing" text, which is what your code snippet does. It's also missing a declaration for `vE`. – Ken White Oct 11 '12 at 20:56
  • +1 Ken, The problem is not replacing text (i must remove all HTML tags which may appear in the word document, but with the following twist: if i encounter formatting tags, such as b, i, u, s, strong, and the like, i have to remove them and format the text accordingly) – beerwin Oct 15 '12 at 12:14