JustinByrom
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.
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.
See https://www.experts-exchange.com/questions/21772822/Delphi-MS-Word-Automation-Finding-text-writing-to-headers.html
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
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
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Appl ication');
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.Exe cute(TextT oFind);
WordApp.Selection.SetRange (SelStart, SelEnd);
WordApp.Selection.copy;
memo1.lines.clear;
Memo1.pastefromclipboard;
WordApp.documents.item(1). Close;
WordApp.Quit;
end;
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.Appl
WordApp.Visible := True;
Wordapp.documents.open('c:
TextToFind := 'Test';
SelStart := WordApp.Selection.Start + Length(TextToFind);
SelEnd := SelStart + 5; // number of character following the found text
WordApp.Selection.Find.Exe
WordApp.Selection.SetRange
WordApp.Selection.copy;
memo1.lines.clear;
Memo1.pastefromclipboard;
WordApp.documents.item(1).
WordApp.Quit;
end;
ASKER
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
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. :)