Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Word Documents->Count in Win32

by esr (Scribe)
on Jan 06, 2005 at 19:47 UTC ( #420064=perlquestion: print w/replies, xml ) Need Help??
esr has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl program which opens a Word document, runs a couple of macros, and then makes the document available for the user to edit and/or print. The program waits for the user to close the document as an indication of being finished. The following snippet mostly works:
# If Word is closed instead of the file, calling "Count" # causes an error message but lowering the Warn level # eliminates it. $mydocnt = $word->Documents->Count(); $word->{Visible} = 1; $word->{WindowState} = wdWindowStateMaximize; $warnlevel = $Win32::OLE::Warn; $Win32::OLE::Warn=1; Win32::MsgBox("After printing, close the file\.",vbOKOnly); $done = 0; while ($done == 0) { if ($word->Documents) { if (($mycnt = $word->Documents->Count()) != $mydocnt) { $done = 1; } } else { undef $word; $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application'); $done = 1; } sleep 5; }
However I discovered that some editing operations cause the "Count" property value to increase temporarily. For example, if I highlight a section of text and then select the "Format" menu in Word, the value of $mycnt increases from 1 to 2 and remains there until I finish with any format changes and click "OK". At that point $mycnt returns to its earlier value of 1.
My impression was that "Documents->Count()" would return the number of documents open but it appears to be something different based on the above. The VBA documentation, such as it is, for the Count property is not real helpful in understanding what is going on.
I changed the if statement in the above snippet to say
if (($mycnt = $word->Documents->Count()) < $mydocnt) {
and that seems to do what I want but I am concerned that I may have problems later, under other circumstances, because I do not understand what is happening here. Perhaps this is more of a Win32::OLE or VBA question but ....

Replies are listed 'Best First'.
Re: Word Documents->Count in Win32
by Zero_Flop (Pilgrim) on Jan 07, 2005 at 07:37 UTC
    This is just a guess, but I bet the system is using a buffered document of some sort to improve performance. This would be similar when you write graphics applications and use buffered images.

    In graphics it takes a lot of resources to display and alter images in real time. So to get around the problem you build an image in the background, then display it. While the system is painting the screen with the current image it can start building the next image.

    But this is just a guess.
Re: Word Documents->Count in Win32
by jplindstrom (Monsignor) on Jan 07, 2005 at 14:16 UTC
    You probably have the Document object handy somewhere (from when you opened the file with
    $doc = $word->Documents->Open(...)
    , or created the document), otherwise you could probably find it in the Documents collection.

    That is probably a more solid way of knowing when the user closed the document.


      Alas, I wish that were true but the Documents object that was saved when the file was opened remains around as long as Word is active. Unfortunately the object reference points to trash. A similar thing happens with the $word object -- i.e. it stays around even if Word is closed but if I try to open another document using that object I get an error. Hence the code in the "else" block to undefine the $word object reference and get a new one. So the code snippet handles both cases where the user closes the document and where s/he closes Word instead.
        Okay, this should work for the case when Word isn't closed. From the docs:

        Named Objects

        Although you can usually specify an integer value with the Item method, it may be more convenient to return an object by name. The following example switches the focus to a document named Sales.doc.


        You know the file name from opening, so when it doesn't return a valid Document object, that would indicate it's closed. Eh... in theory :)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://420064]
Approved by Arunbear
and the grasshoppers chirp...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-12-17 20:33 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (466 votes). Check out past polls.