http://www.perlmonks.org?node_id=420443


in reply to Re: Word Documents->Count in Win32
in thread Word Documents->Count in Win32

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.

Replies are listed 'Best First'.
Re^3: Word Documents->Count in Win32
by jplindstrom (Monsignor) on Jan 08, 2005 at 02:21 UTC
    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.

    Documents("Sales.doc").Activate

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

    /J