Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2002-06-13
18
Medium Priority
?
228 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
[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
  • 9
  • 9
18 Comments
 
LVL 3

Accepted Solution

by:
lopem earned 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

636 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