Solved

How can I get all text in a word document or in a excel file?

Posted on 2002-06-13
18
214 Views
Last Modified: 2010-04-04
hi friend
  I tried automation component on SERVER tab, but failed. can you give me some code?

thanx
0
Comment
Question by:chengjian
  • 9
  • 9
18 Comments
 
LVL 3

Accepted Solution

by:
lopem earned 100 total points
ID: 7075984
Hi Chengian,

to find out how to use OLE with Word, see Delphi example... You can find it normally in

     C:\Program Files\Borland\Delphi5\Demos\Activex\Oleauto\Word8

To read all the text of a document try the code shown below... If you want to add more functionality my best advice is to record a macro in Word and find out the macro code. I found this technique very useful when I want to port the code to delphi...

best regards
Manuel Lopez (lopem)

procedure InsertText(Pal : string);
//inserta text in Word
var
   Sel : Selection;
begin
  Sel := Form1.MsWord.Selection;
  Sel.TypeText(Pal);
end;

procedure MovePreviousWord;;

var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
begin
  MoveUnit := wdWord;
  vCount := 1;
  Extended := unassigned;
  Form1.MsWord.Selection.MoveLeft(MoveUnit, vCount, Extended);
end;

procedure MoveNextWord;
//move cursor to next word
var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
begin
  MoveUnit := wdWord;
  vCount := 1;
  Extended := unassigned;
  Form1.MsWord.Selection.MoveRight(MoveUnit, vCount, Extended);
end;

procedure ReadWord;
//read a word from Word doc
var
   Pal      : string;
   i        : integer;
begin
  ActRange := Form1.MsWord.Selection.Words.Item(1);
  Pal := ActRange;
end;

procedure CountAllWordOnDoc(var Cuenta: integer);
var
   Tot : OleVariant;
   Total : integer;
begin
  Tot := Form1.MsWord.ActiveDocument.Words.Count;  
  Total := Tot;
  Cuenta := Total;
end;

procedure CountWordsHighLighted(var Cuenta : integer);

var
   Tot : OleVariant;
   Total : integer;
begin
  Tot := Form1.MSWord.Selection.Words.Count;
  Cuenta := Tot;
end;

procedure HighLightWord;
//HighLight a word

var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
begin
  MoveUnit := wdWord;
  vCount := 1;
  Extended := wdExtend;
  Form1.MsWord.Selection.MoveRight(MoveUnit, vCount, Extended);
end;


procedure HomeCursor;
//put cursor at the beginning of the document
var
   Unidad : OleVariant;
   Extended : OleVariant;
begin
  Unidad := wdStory;
  Extended := Unassigned;
  Form1.MsWord.Selection.HomeKey(Unidad,Extended);
end;





procedure TForm1.Button1Click(Sender: TObject);
//check if you can read word by word, from beginning to end of document...
//this test will change all word from lower to UPPER case...

var
   Pal      : string;
   i        : integer;
   Total    : integer;
begin
  HomeCursor;
  CountAllWordOnDoc(Total);
  for i := 1 to Total do
  begin
    ActRange := MsWord.Selection.Words.Item(1);
    if i <> 1 then HighLightWord
              else
              begin
                HomeCursor;
                HighLightWord;
              end;
    Pal := ActRange;
    Pal := UpperCase(Pal);
    //ShowMessage(Pal);
  end;
end;




0
 
LVL 1

Author Comment

by:chengjian
ID: 7078237
Thanx a lot, but what can i do on a password protected document?

thanx again.
0
 
LVL 3

Expert Comment

by:lopem
ID: 7078491
Hi Chengjian,

To my best knowledge you can't do nothing to a password protected document without the password. I dont know (if there is a chance) how to deal with password protected documents.

Best regards,
Manuel Lopez (lopem)
0
 
LVL 1

Author Comment

by:chengjian
ID: 7101624
Sorry for my late reply.
I means, if I know the password, how to open the file? with the KNOWN password?

thanx.
0
 
LVL 3

Expert Comment

by:lopem
ID: 7102128
Hi Chengjian,

If you have the password of the document, you can open a document from your delphi application using this sort of procedure (assuming you're using properly the OLE automation):

procedure ReadDocumentFromWord;
begin
  MsWord.Dialogs.Item(wdDialogFileOpen).Show(EmptyParam);
end;

If the document is password protected, you can just write the password on the dialog box showerd by MsWord itself. I dont know how to make it automatically and Im looking the way to do this with some sort of macro.  But I dont know if there is a way to do that.

best regards
Manuel Lopez (lopem)
0
 
LVL 1

Author Comment

by:chengjian
ID: 7103505
hi
Oh, I see, I'll end the question after review the code.
Thanx again
0
 
LVL 1

Author Comment

by:chengjian
ID: 7116345
hi

I've reviewed the code , it really give me some good advice.
Now I use the code following to get all Text in word document:

procedure TForm1.Button1Click(Sender: TObject);
var
  Total    : integer ;
  fn:        OleVariant;
  b:         OleVariant;
  s:         String;
  vT,vC, vE:      OleVariant;
  bContinue:  Boolean;
begin
  fn := 'D:\Documents and Settings\Administrator\My Documents\¬ÍåÇøÊÇÉϺ£µÄÖÐÐijÇÇø£¬.doc';
  b := false;
  s := '';
  bContinue := true;
  while bContinue do
    try
      MsWord.Connect;
      bContinue := false;
    except
      bContinue := YesNoBox('Connect to word application failed, would you like to retry?');
      if not bContinue then
        exit;
    end;

 MsWord.Application.Visible := false;
 MsWord.Documents.Open(fn,b,b,b,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam );
 MsWord.Visible := false;
 HomeCursor;
 CountAllWordOnDoc(Total);
 vT := Total;
 vC := wdCharacter;
 vE := wdExtend;
 HighLightWord;
 MsWord.Selection.MoveRight(vC, vT,vE);
 ActRange := MsWord.Selection.Text;
 Memo1.Lines.Add(ActRange);
 MsWord.ActiveDocument.Close(b, b, b);
 MsWord.Disconnect;
 MsgBox('Word application disconnected.');
end;

The problem is :
1.EVERY TIME I RUN THE PROGRAM
When the first time I open a document, it always raise exception, but I click the button again, it works:(

2.When there're some tables, OLE objects in the document, NOT ALL the text are selected:(

Can u help me?

thanx again
0
 
LVL 3

Expert Comment

by:lopem
ID: 7119467
Hi there...

To execute Word I dont use your method. I just did it this way:

try
    MSWord.Connect;
  except on E : Exception do
    begin
      E.Message := 'Word is not available';
      raise;
    end;
end;

If Word is not available the first time, it makes no sense to ask it to retry, right? I didnt have the problem of failing to load Word using the code above.

Later, on your code, I dont see any loop to get all the words to the memo (unless you modified the highlight code to do that). Maybe Im not understanding too well what do you want to do exactly.

Nevertheless, I understand word handles tables and other objects in some specific ways. If you want to handle a table from delphi try first to record a macro in Word to see how Word itselfs handles it. You can see the code pressing alt-f11.

I dont know how to handle differents OLE objects from delphi. Probably you have to dig a little in the macro reference.

best regards
Manuel Lopez (lopem)
0
 
LVL 1

Author Comment

by:chengjian
ID: 7119560
Hi

I use the code to open word, becaosue if the word is not running, the first time calling connect mathod would fail, but second time would success. so I use a loop to ask user to confirm if re-connect word.

>vT := Total;
>vC := wdCharacter;
>vE := wdExtend;
>MsWord.Selection.MoveRight(vC, vT,vE);
This means from start(homecursor) moveRight Total Character andextend the selection area.

>ActRange := MsWord.Selection.Text;
This means Get all Text in selection, we do not need word by word.

Best Regards
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 3

Expert Comment

by:lopem
ID: 7119569
Hi,

did you try my code to open MsWord? In my case, Word is not running when I call it. But with my code I didnt get any error message.

best regards
Manuel Lopez (lopem)
0
 
LVL 1

Author Comment

by:chengjian
ID: 7124344
hi

At the begainning, I used the code u giving above, but...

Last question, how can I get Author, Title ect. ?
I know there is a DocumentProperty stuff. how can I get these?

I promis another 100.

best regards
0
 
LVL 3

Expert Comment

by:lopem
ID: 7124936
hi chengjian,

Let me see how you can get that info... I have to find out... give me few days.

best regards
Manuel Lopez (lopem)
0
 
LVL 3

Expert Comment

by:lopem
ID: 7125912
Hi there.

I had no time to try this but I guess it will work properly:

This example returns the author name for the first comment in the selection.

If MsWord.Selection.Comments.Count >= 1 Then
    aAuthor := Selection.Comments(1).Author

aAuthor, I guess, must be Olevariant.

hope this helps,
best regards,
Manuel Lopez (lopem)

Ps. I didnt find a way to extract all the information. I dont have the complete reference for visualbasic for MsWord, so I cant help you more in this subject. I mean, probably in the complete reference of Word you will find all this info but I dont have it here.
0
 
LVL 1

Author Comment

by:chengjian
ID: 7126421
Hi

You can find that help in MS Office 2k Professional's directory, it's name like 'vbword9.chm'

I know the way u give, but the author of a comment is different from an author of document.

Best Regards.
0
 
LVL 3

Expert Comment

by:lopem
ID: 7127134
Oh... well, I have word97 version. Let me try to find more info.

best regards,
Manuel Lopez (lopem)
0
 
LVL 1

Author Comment

by:chengjian
ID: 7132273
OK, Thanx. I'll end the question for few days.

best regards
0
 
LVL 3

Expert Comment

by:lopem
ID: 7132340
hi Chengjian,

Sorry, I didnt find any way to get the info you want about a document... Maybe I dont have the reference needed. I cant help you here.

Best regards,
Manuel Lopez (lopem)
0
 
LVL 1

Author Comment

by:chengjian
ID: 7134470
OK

Thanx again
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento 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

11 Experts available now in Live!

Get 1:1 Help Now