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 ....