Problems using Search and Replace on Office 2007

Giganews Newsgroups
Subject:Problems using Search and Replace on Office 2007
Posted by: Bjorn Johansen (…@ssv.dk)
Date:Fri, 24 Jul 2009

Hi,

I have an application written in Delphi 7, using Word-integration on
Office 2003. Now the customer wants to move to Office 2007, and now
Search and Replace no longer works.

The following code-snippet illustrates the problem:

var
  wDoc,
  aVisible : OleVariant;
  wTekst  : OleVariant;

  Procedure SearchAndReplace(SearchText, ReplaceText: String);
  var
    sTxt, rTxt,
    aMatchCase,
    aMatchWholeWord,
    aWrap,
    aReplace        : OleVariant;
  begin
    aMatchCase := false;
    aMatchWholeWord := true;
    aWrap := wdFindContinue;
    aReplace:= wdReplaceAll;

    sTxt := SearchText;
    rTxt := ReplaceText;
    if WordApplication1.Selection.(*Range.*)Find.Execute
        (sTxt, aMatchCase,
          // FindText, MatchCase
          aMatchWholeWord, EmptyParam,
          // MatchWholeWord, MatchWildcards
          EmptyParam, EmptyParam,
          // MatchSoundsLike, MatchAllWordForms
          EmptyParam, aWrap, EmptyParam,
          // Forward, Wrap, Format
          rTxt, aReplace,
          // ReplaceWith, Replace
          EmptyParam, EmptyParam,
          // MatchKashida, MatchDiacritics
          EmptyParam, EmptyParam) then
          // MatchAlefHamza, MatchControl
      showmessage('replaced')
    else
      showmessage('not replaced');
  end;

begin
  WordApplication1.Connect;
  WordApplication1.Visible := True;
  aVisible := True;
  wDoc := 'C:\test.doc';
  WordDocument1.ConnectTo(WordApplication1.Documents.Add(
          wDoc,EmptyParam,EmptyParam,aVisible));
  wTekst := 'This is a test';
  WordApplication1.Selection.TypeText(wTekst);
  // Works fine on both platforms
  SearchAndReplace('Replace this text','with that text');
  // Works fine on Office 2003, but fails on Office 2007 with "stub
received bad data" (translated from danish - maybe invalid data?)
end;

Does anyone have a clue about whats wrong? Anything else in automation
seems to work OK.

Thanks in advance.

Bjorn

Replies