Recent

Author Topic: Word and Page Count from MS Word  (Read 1128 times)

w click

  • Full Member
  • ***
  • Posts: 137
Word and Page Count from MS Word
« on: September 20, 2021, 05:03:57 pm »
I want to extract the word and page count from a Microsoft Word document.  In Delphi, I used MSWordAndPageCount(filename,words,pages) which was part of MSWordUnit.  No amount of searching reveals the Lazarus equivalent.  Can anyone advise, please?

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1063
Re: Word and Page Count from MS Word
« Reply #1 on: September 20, 2021, 05:16:48 pm »
hello,
to do that only on windows ?

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1063
Re: Word and Page Count from MS Word
« Reply #2 on: September 20, 2021, 06:19:29 pm »
on Windows with msword installed you can  try this :
Code: Pascal  [Select][+][-]
  1. uses ComObj, Variants;
  2. procedure TForm1.Button3Click(Sender: TObject);
  3. const mydoc = 'd:\temp\LazWord.docx';
  4. var msWord,sourceDoc : OleVariant;
  5. var nbPages,nbWords : integer;
  6. begin
  7. msWord := CreateOleObject('Word.Application');
  8. // wdStatisticLines     1       Count of lines.
  9. // wdStatisticPages     2       Count of pages.
  10. // wdStatisticParagraphs        4       Count of paragraphs.
  11. // wdStatisticWords     0       Count of words.
  12. sourceDoc := msWord.Documents.Open(mydoc);
  13. nbPages := sourceDoc.ComputeStatistics(2);
  14. nbWords := sourceDoc.ComputeStatistics(0);
  15. ShowMessage('File ' + mydoc + #13#10 + 'nb Pages : ' + IntToStr(nbPages) +
  16.             #13#10 + 'nb Words : ' + IntToStr(nbWords));
  17. sourceDoc.Close;
  18. msWord.quit;
  19. end;            
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

w click

  • Full Member
  • ***
  • Posts: 137
Re: Word and Page Count from MS Word
« Reply #3 on: September 21, 2021, 11:54:41 am »
J.P.,
That works - many thanks.  Except that it reveals perhaps a few issues.  I was using something similar to simply open the document in Word and so I've three questions.

1. I was using a variant, not an OleVariant.  What's the difference?
2. I only used one and just called 'msWord.Documents.Open(mydoc);'.  So why assign it to sourceDoc?
3. I didn't 'sourceDoc.close' or 'msWord.quit' as I wanted to keep Word open.  Should I quit at least?

And it's about three times slower!

Cheers,
David.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1063
Re: Word and Page Count from MS Word
« Reply #4 on: September 21, 2021, 12:27:33 pm »
hello,
1. I was using a variant, not an OleVariant.  What's the difference?
2. I only used one and just called 'msWord.Documents.Open(mydoc);'.  So why assign it to sourceDoc?
3. I didn't 'sourceDoc.close' or 'msWord.quit' as I wanted to keep Word open.  Should I quit at least?

And it's about three times slower!


1- Property: The data type OleVariant is a variant data type that is used for OLE automation (automation of other programs).
2 - to have a document automation object.
3 - If you don't see MS Word windows, you don't know  if it is already opened  ---> if you want to see MsWord window add this :
Code: Pascal  [Select][+][-]
  1. msWord.visible := True;  
If msWord isn't visible don't forget to quit it when you close your application.



Friendly, J.P   


« Last Edit: September 21, 2021, 12:35:37 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

w click

  • Full Member
  • ***
  • Posts: 137
Re: Word and Page Count from MS Word
« Reply #5 on: September 21, 2021, 03:19:43 pm »
J.P.,

Grand, thank you.

I knew the msword.visible bit, but much appreciated to know I'm on the right lines.  The program was just to fire up a Word document without faffing with finding the file, but I'm getting more ambitious.

I think I should change Variant to OleVarient.

And use 'msWord.quit'?  I'm guessing without it, it's endlessly filling memory up with OleVariants.  Or is sourcedoc.close needed too?  Basically, it fires up Word, I edit things and then quit from Word.  The program just sits in the background waiting for next time.  I'm not sure I can always remember to go back to the launching program to quit Word.

Cheers,

David.

 

TinyPortal © 2005-2018