Recent

Author Topic: A little help with managing Microsoft Word from within a Lazarus program  (Read 4969 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
I have a Windows specific request. I need to:

  • Open a word document -- Done, know how to do that
  • Search for a specific string and move the cursor in Word to that location.
I know how to accomplish 1. I'm stumped with 2. Here's what I know: The Document object in Word has a Selection[//tt] object. In turn, Selection has a Find object which can be used to search. Find, according to this article on msdn.microsoft.com, requires the following parameters:

Code: Text  [Select][+][-]
  1. With Selection.Find
  2.  .Forward = True
  3.  .ClearFormatting
  4.  .MatchWholeWord = True
  5.  .MatchCase = False
  6.  .Wrap = wdFindContinue
  7.  .Execute FindText:="Microsoft"
  8. End With
  9.  

Question: How do I turn this VBA code into a pascal code relying on variants?. I'm facing at least two challenges with the faulty code below:
Code: Pascal  [Select][+][-]
  1.   fSelection := fDocument.Selection;
  2.   With fSelection.Find do
  3.   begin
  4.      Forward := True               ; //<== Won't compile
  5.      ClearFormatting               ;
  6.      MatchWholeWord := True        ;
  7.      MatchCase := False             ;
  8.      Wrap := wdFindContinue         ;
  9.      Execute FindText:="Microsoft" ; //<== won't compile.
  10.   end;
  11.  

Any suggestion you might have here would be helpful.

Also, As I go, I'm building a set of classes to wrap all this wonderful code behind a streamlined facade. If anyone is interested, let me know and I'll post it back here.

Thanks

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: A little help with managing Microsoft Word from within a Lazarus program
« Reply #1 on: October 23, 2017, 05:54:57 am »
Just guessing, how about:
Code: Pascal  [Select][+][-]
  1.   W := CreateOLEObject('Word.Application');
  2.   S := W.Selection;
  3.   F := S.Find;
  4.   //With fSelection.Find do
  5.   begin
  6.      F.Forward := True               ;
  7.      F.ClearFormatting               ;
  8.      F.MatchWholeWord := True        ;
  9.      F.MatchCase := False             ;
  10.      F.Wrap := wdFindContinue         ;
  11.      F.Execute(FindText := 'Microsoft');
  12.   end;

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: A little help with managing Microsoft Word from within a Lazarus program
« Reply #2 on: October 25, 2017, 06:32:20 am »
So, yeah, that's not going very well ...
Here's my latest attempt:
Code: Pascal  [Select][+][-]
  1. function TaiWordDocument.GoToText(const aText: String): Boolean;
  2. var C,F   : Variant;
  3.     WText : Variant;
  4. begin
  5. //  C := fDocument.Content       ;
  6.   C := fDocument.Range(0 , 0);
  7.   F := C.Find                  ;
  8.   F.Forward        := True     ;
  9.   F.ClearFormatting            ;
  10.   F.Highlight      := true     ;
  11.   F.MatchWholeWord := false    ;
  12.   F.MatchCase      := False    ;
  13.   F.Wrap           := wdFindContinue;
  14.   wText            := aText    ;
  15.   Result := F.Execute({FindText         } wText      ,
  16.                       {MatchCase        } EmptyParam ,
  17.                       {MatchWholeWord   } EmptyParam ,
  18.                       {MatchWildcards   } EmptyParam ,
  19.                       {MatchSoundsLike  } EmptyParam ,
  20.                       {MatchAllWordForms} EmptyParam ,
  21.                       {Forward          } EmptyParam ,
  22.                       {Wrap             } EmptyParam ,
  23.                       {Format           } EmptyParam ,
  24.                       {ReplaceWith      } EmptyParam ,
  25.                       {Replace          } EmptyParam ,
  26.                       {MatchKashida     } EmptyParam ,
  27.                       {MatchDiacritics  } EmptyParam ,
  28.                       {MatchAlefHamza   } EmptyParam ,
  29.                       {MatchControl     } EmptyParam
  30.                      );
  31.   //F.Execute(FindText := wText  );
  32. end;
  33.  

The method returns true but Microsoft Word does not move its focus to the string I'm searching. There's no difference if I use content or range. If anyone else has an idea, would be happy to consider it.

 

TinyPortal © 2005-2018