Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 ....

In reply to Word Documents->Count in Win32 by esr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found