Solved

Extract text from Word document ...

Posted on 2006-06-26
7
556 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:JustinByrom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16982654
0
 
LVL 1

Author Comment

by:JustinByrom
ID: 16982817
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
0
 
LVL 1

Author Comment

by:JustinByrom
ID: 16982826
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 250 total points
ID: 16982976
Here's a demo. In my demo I just create the document on the fly but in your case you would probably do wa.Documents.Open to load the file.

PAS File:
=============================================================
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  wdMove          = 0;
  wdExtend        = 1;

  wdCharacter     = 1;
  wdParagraph     = 4;
  wdLine          = 5;
  wdStory         = 6;

  wdFindStop      = 0;
  wdFindContinue  = 1;
  wdFindAsk       = 2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

uses ComObj;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var wa, wd, selection: Variant;
    s: string;
begin
  wa:=CreateOleObject('Word.Application');
  wa.Visible:=True;
  wd:= wa.Documents.Add();
  wd.Select;
  wa.Selection.TypeText('blah blah findtext1=value1;'#13#10);
  wa.Selection.TypeText('blah findtext2=value2; blah'#13#10);
  wa.Selection.TypeText('blah blah findtext3=value3;'#13#10);
  wa.Selection.TypeText('blah findtext4=value4;'#13#10);

    wa.Selection.Find.ClearFormatting;
    wa.Selection.Find.Text:='findtext2=';
    wa.Selection.Find.Replacement.Text:= '';
    wa.Selection.Find.Forward:= true;
    wa.Selection.find.Wrap:= wdFindContinue;
    wa.Selection.Find.Format:= False;
    wa.Selection.Find.MatchCase:= False;
    wa.Selection.Find.MatchWholeWord:= False;
    wa.Selection.Find.MatchWildcards:= False;
    wa.Selection.Find.MatchSoundsLike:= False;
    wa.Selection.Find.MatchAllWordForms:= False;
    wa.Selection.Find.Execute;
    wa.Selection.MoveRight(wdCharacter, 8, Extend:=wdExtend);

  s:= wa.Selection.range.text;
  ShowMessage(s);
end;

end.


DFM File:
=================================================================
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 32
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end


Regards
Pierre
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16983103
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;
0
 
LVL 1

Author Comment

by:JustinByrom
ID: 16983140
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
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16983168
That's fine. :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tidtcpserver connection lost handle 2 124
tvirtualstringtree freeze when load too manny images 10 94
IP without any Dots 1 99
QuickReport, Delphi, Shape 12 49
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question