Link to home
Start Free TrialLog in
Avatar of JustinByrom
JustinByromFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Extract text from Word document ...

Hi,

I have a client who needs me to write a small tool that reads in a Word document, searches for a specific text string, reads in some text immediately following it, and close it without making any changes.

All I can find are lots of examples on how to open a word document and write data, or replace strings, etc.

Can anybody help ?

The OLEContainer methods are not very well documented, which makes it difficult to try anything on the fly.
Avatar of atul_parmar
atul_parmar
Flag of India image

Avatar of JustinByrom

ASKER

Atul,

I've looked at this one, but it concentrates on tables.  I have a full text document, and need to find 'sampletext' and then read a number of characters after that.

Justin
Further to the initial post, I am also having problems including the Word_TLB.pas file.

It falls over at the 'FOnXMLBeforeDelete' call within the InvokeEvent (...) procedure.

I think it has something to do with the fact that I'm compiling under Delphi 7, but have Delphi 8.NET installed (which was installed after D7).  I am looking into this currently.
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

Use the following code. it does not require Word_TLB.pas to be included.

var
  WordApp : Variant;
  TextToFind : String;
  SelStart, SelEnd : integer;
begin
  WordApp := CreateOleObject('Word.Application');
  WordApp.Visible := True;
  Wordapp.documents.open('c:\Test.doc');
  TextToFind := 'Test';
  SelStart := WordApp.Selection.Start + Length(TextToFind);
  SelEnd := SelStart + 5; // number of character following the found text
  WordApp.Selection.Find.Execute(TextToFind);
  WordApp.Selection.SetRange(SelStart, SelEnd);
  WordApp.Selection.copy;
  memo1.lines.clear;
  Memo1.pastefromclipboard;
  WordApp.documents.item(1).Close;
  WordApp.Quit;
end;
Atul,

Thanks for your reply, but Pierre was first and I have used his solution (and modified it to suit) successfully.  I think it is only fair to award him all the points, although your solution is more concise.

Thanks very much,

Justin
That's fine. :)