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 ....
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.