Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-01-2019, 10:57 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Arrow 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.
Reply With Quote
  #2  
Old 01-03-2019, 01:21 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default

Anyone?
Reply With Quote
  #3  
Old 01-03-2019, 02:08 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,335
Default

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
Reply With Quote
  #4  
Old 01-03-2019, 02:22 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default

Quote:
Originally Posted by Norrit View Post
and translating to Delphi
Now if only I'd understand VBA code

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

Last edited by tcsh; 01-03-2019 at 02:43 PM.
Reply With Quote
  #5  
Old 01-04-2019, 09:53 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,335
Default

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
Reply With Quote
  #6  
Old 01-05-2019, 05:26 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Thumbs up

Quote:
Originally Posted by Norrit View Post
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 12:05 AM.


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