Solved

Extract text from Word document ...

Posted on 2006-06-26
7
541 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now