Delphi Pages Forums

Delphi Pages Forums (http://www.delphipages.com/forum/index.php)
-   General (http://www.delphipages.com/forum/forumdisplay.php?f=2)
-   -   Open .doc - search and copy some text (http://www.delphipages.com/forum/showthread.php?t=218654)

tcsh 01-01-2019 10:57 PM

Open .doc - search and copy some text
 
Hi,

I have a few thousand .doc files all filled in with data following a pretty simple pattern/template.

Example:
Code:

NAME:
SURNAME:
AGE:
ADDRESS:
DATE:

I'd like to open the .doc in my delphi application, search for NAME: then identify, select and copy to clipboard EVERY BIT of text that starts right after NAME: all the way to where SURNAME: starts.

Example:
Code:

NAME: JOSH
SURNAME: JIM

It should copy JOSH to the clipboard.

Example #2:

Code:

NAME: JOSH
JOSH
SURNAME: JIM

It should copy JOSH JOSH to the clipboard.

Example #3:
Code:

NAME: JOSH          JOSH
JOSH
SURNAME: JIM

It should copy JOSH JOSH JOSH to the clipboard.

Basically, should start copying everything starting right after : from the NAME: and stop right before S from/where SURNAME: starts.

Code so far:

Code:

procedure TForm1.Button2Click(Sender: TObject);
var
  WordApp : Variant;
  TextToFind : String;
  i: integer;
  fname: string;

 begin
  WordApp := CreateOleObject('Word.Application');
  WordApp.Visible := True;
  for i := 1 to 3 do

  begin
  fname := Memo1.Lines[i - 1];
  Wordapp.Documents.Open('C:\Users\tcsh\Desktop\A\'+fname);
  TextToFind :='NAME:';
  WordApp.Selection.Find.Execute(TextToFind);
  WordApp.Selection.Copy;
  Memo2.SetFocus;
  Memo2.PasteFromClipboard;
  end;

 end;

Managed to locate the .doc, open it, find NAME: and select it, copy it and paste it in Memo2 from the clipboard. Obviously, I need the text after NAME: as explained above and that's where I'm stuck.

Could anyone add to the code so I can get this done ?

Appreciate it. :)

tcsh 01-03-2019 01:21 PM

Anyone? :confused::p

Norrit 01-03-2019 02:08 PM

In VBA your code could look similar to:
Code:

    Dim i1 As Long
    Dim search1 As String
    search1 = "NAME:"
    Dim i2 As Long
    Dim search2 As String
    search2 = "SURNAME:"
   
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Wrap = wdFindStop
        .MatchCase = False
        .Text = search1
        .Execute
        Selection.Collapse Direction:=wdCollapseEnd
        i1 = Selection.End
        .Text = search2
        .Execute
        i2 = Selection.Start
    End With
    ActiveDocument.range(i1, i2).Copy

Only thing you need to do is some error handling (i2 > i1) and translating to Delphi

tcsh 01-03-2019 02:22 PM

Quote:

Originally Posted by Norrit (Post 235188)
and translating to Delphi

Now if only I'd understand VBA code :confused:

Thanks for your reply. I'll try to piece something together out of that.

yup, getting errors on wdCollapseEnd, wdStory and the Selection.End is breaking up my procedure.

Could you write it in Delphi? Its my 1st time using msword in Delphi and I have no clue how to write those. All I keep getting on the google searches are .vba code which I don't understand properly

Norrit 01-04-2019 09:53 AM

I have no Delphi anymore, so I cannot rewrite this

Bug the wd's are just constant values, a quick google will give the integer equivalent
https://docs.microsoft.com/en-us/pre...3(v=office.11)

As for the With statement, that's the same as in Delphi, so you should capture it's output in a Variant and call each next function on that variant
Perhaps the ClearFormatting, Wrap and all the others are available in an overload on the Execute statement, but for that you'll have to dive into the specs of that call (in other words, look in the msdn)

But basically it's pretty straight forward to translate this into Delphi. The Dim's should be integer variables in Delphi and the string's are strings in Delphi (like your own TextToFind), and you'll need another Variant to replace the With and take the output of the .Find

tcsh 01-05-2019 05:26 PM

Quote:

Originally Posted by Norrit (Post 235190)
I have no Delphi anymore, so I cannot rewrite this

Bug the wd's are just constant values, a quick google will give the integer equivalent
https://docs.microsoft.com/en-us/pre...3(v=office.11)

As for the With statement, that's the same as in Delphi, so you should capture it's output in a Variant and call each next function on that variant
Perhaps the ClearFormatting, Wrap and all the others are available in an overload on the Execute statement, but for that you'll have to dive into the specs of that call (in other words, look in the msdn)

But basically it's pretty straight forward to translate this into Delphi. The Dim's should be integer variables in Delphi and the string's are strings in Delphi (like your own TextToFind), and you'll need another Variant to replace the With and take the output of the .Find

thanks for the reply! getting right on it


All times are GMT. The time now is 02:32 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.