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

hi friend
  I tried automation component on SERVER tab, but failed. can you give me some code?

thanx
LVL 1
chengjianAsked:
Who is Participating?
 
lopemConnect With a Mentor Commented:
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
 
chengjianAuthor Commented:
Thanx a lot, but what can i do on a password protected document?

thanx again.
0
 
lopemCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
chengjianAuthor Commented:
Sorry for my late reply.
I means, if I know the password, how to open the file? with the KNOWN password?

thanx.
0
 
lopemCommented:
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
 
chengjianAuthor Commented:
hi
Oh, I see, I'll end the question after review the code.
Thanx again
0
 
chengjianAuthor Commented:
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
 
lopemCommented:
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
 
chengjianAuthor Commented:
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
 
lopemCommented:
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
 
chengjianAuthor Commented:
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
 
lopemCommented:
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
 
lopemCommented:
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
 
chengjianAuthor Commented:
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
 
lopemCommented:
Oh... well, I have word97 version. Let me try to find more info.

best regards,
Manuel Lopez (lopem)
0
 
chengjianAuthor Commented:
OK, Thanx. I'll end the question for few days.

best regards
0
 
lopemCommented:
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
 
chengjianAuthor Commented:
OK

Thanx again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.