Link to home
Start Free TrialLog in
Avatar of delphiDude2011
delphiDude2011

asked on

Help! Having performance problems using MS Word's search/Replace function in Delphi 6

Help!

I am writing a program at the moment that needs to generate an MS word doc from Delphi, and also merge in a seperate MS Word document a number of times, each time replacing a special tag with a different number.  The merging part I have no problem with but I am experiencing performance problems with the number of times I am calling Word's search/Replace function, and its taking way too long to generate the finished document.

The search/replace function gets called about 46 times per merge, with 20 merges thats 920 calls to the function (each one taking 80msec on average). I ideally would rather not call word's function at all and somehow do the search/replace in memory, but when I tired to do write some code that did this, some of the special tags in document being merged got lost (I think due to the way I am getting the text).

The code I hoped would work is in the snippet below.

The problem is the document I am search/replacing has tables and lots of mail merge fields and IF conditions and so on, so these special characters are getting lost. Is there some way I can modify this code so I dont loose the special characters used by word, and therefore bypass the performance problems associated with using the native word search/replace function?

Thanks!
var 
     aDocText : String; 
     AutoDoc : Word_TLB._Document;
     Field : Word_TLB.Field
     TargetStart : OleVariant;
     TargetRange :Word_TLB.Range;
begin
....  
      // get the DWInsert Field Position (in the main doc)
      TargetStart := Field.Code.Start;
      Field.Delete;
      TargetRange := MergeDoc.Range(TargetStart, TargetStart);
      aDocText := AutoDoc.Content.Text;
      // replace all occurances of MERGEFIELD 'nx' with 'n1', etc in memory
      ProcessDocText(aDocText,Params[2],Params[3]);

      // Now copy the new merge document contents to the main document
     // HERE IS THE PROBLEM - WHEN TEXT IS COPIED THIS WAY, FORMATTING IS LOST
      TargetRange.Text := aDocText; 
.....

procedure TDwMicrosoftWordCtrl.ProcessDocText(
  var aDocText : String;
   aSearchTag : String;
   aReplaceTag: String);
var
   aDocTagList   : TStringList;
   anIndex       : Integer;
   anActualPos   : Integer;
   anExpectedPos : Integer;
   aFieldText    : String;
   aReplaceString: String;
begin
     Assert(length(aSearchTag) > 0);
     Assert(length(aReplaceTag) > 0);
     aDocTagList := TStringList.Create;
     // tokenise doc text by field tags. We are looking for MERGEFIELD tags
     vStringToStringList(aDocText,aDocTagList,[#19,#21]);

     for anIndex := 0 to aDocTagList.Count -1 do
     begin
          aFieldText := Trim(aDocTagList[anIndex]);
          if Pos('MERGEFIELD ', Uppercase(aFieldText)) = 1 then
          begin
               anExpectedPos := Length(aFieldText) - Length(aSearchTag);
               anActualPos := Pos(aSearchTag,aFieldText) - 1;
               if (anActualPos = anExpectedPos) then
               begin
                    // If tag found in mergefield, replace aSearchText with aReplaceText
                    aReplaceString := Copy(aFieldText,0, anActualPos);
                    aReplaceString := aReplaceString + aReplaceTag;
                    aDocText := StringReplace(aDocText,AFieldText,aReplaceString,[rfReplaceAll]);
              end;
          end;
     end;
     aDocTagList.Free;
end;

Open in new window

Avatar of aikimark
aikimark
Flag of United States of America image

Why not pass a string of name=value pairs that you iterate within a TStringList?  You only make one call to the routine.

What version of Word are you using?  Delphi 6 might be having some trouble with a newer version of Word.

What replacements are being made within a document?

Is this a mailmerge operation?
ASKER CERTIFIED SOLUTION
Avatar of delphiDude2011
delphiDude2011

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of delphiDude2011
delphiDude2011

ASKER

ok
By calling the search replace function once for the whole document this runs much faster, and the coversion in memory solution is then not needed.