Getting error when I am replacing text in word 2013 document.

When my program is replacing text in word documents, using Delphi XE7 It get this error, „This command is not available“  It only happens with few users. Some users can do it with no errors.
Users have all Windows 7 and office 2013.

First we open the document.
procedure TfrmAttachmentsDOA.OpenWord;
  if VarIsClear(FApp) then
    FApp := CreateOleObject('Word.Application');
    FApp.Visible := false;
  FDoc := FApp.Documents.Open( FSkraarheiti );

Open in new window

Then we replace the text we get the error „This command is not available“
The question is way is the document read only at this time?
procedure FindReplaceAll(strFinna, strReplace: OleVariant; OleObj: Variant);
   strResponse: string;
   wdFindContinue, wdReplaceAll: integer;
   count : integer;
   count := 0;
   wdFindContinue  := 1;
   wdReplaceAll    := 2;

   while ( length(strReplace)  > 0 ) or ( count = 0 ) do
      count := count + 1;
      if length(strReplace) > 251 then
         strResponse := AnsiMidStr ( strReplace, 1, 220) + strFinna;
         strReplace := AnsiMidStr( strReplace, 221, length(strReplace));
         strResponse := strReplace;
         strReplace := '';

      OleObj.application.Selection.Find.Text := strFinna;
      OleObj.application.Selection.Find.MatchCase := False;
      OleObj.application.Selection.Find.MatchWholeWord := False;
      OleObj.application.Selection.Find.MatchWildcards := False;
      OleObj.application.Selection.Find.MatchSoundsLike := False;
      OleObj.application.Selection.Find.MatchAllWordForms := False;
      OleObj.application.Selection.Find.Forward := True;
      OleObj.application.Selection.Find.Wrap := wdFindContinue;
      OleObj.application.Selection.Find.Format := True;
      OleObj.application.Selection.Find.Replacement.Text := strResponse;
     //Here we get the error.
      OleObj.application.Selection.Find.Execute(Replace := wdReplaceAll);

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't have an up-to-date version of Delphi and have forgotten most of it anyway, but I suggest that you pass the document object variable (Fdoc) into the procedure and work on its main range, rather than relying on the Selection, since you cannot always be sure that the Selection is the target that you need.

The code can be a lot simpler, because you create  a new Find object, so that all the settings (Wildcards, formatting, etc) will be at their default values. There is only one Selection.Find object per application instance, so the values persist.

Your code would be something like this:
rngFind: word.Find;

      rngFind := FDoc.Range.Find
      rngFind.Text := strFinna;
      rngFind.Replacement.Text := strResponse;
      rngFind.Execute(Replace := wdReplaceAll);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.