in reply to Word Documents->Count in Win32

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.

/J

Replies are listed 'Best First'.
Re^2: Word Documents->Count in Win32
by esr (Scribe) on Jan 08, 2005 at 01:09 UTC
    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.

      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