From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Fun with MS Word

I'm trying to cobble together a VI that will, via ActiveX and MS Word2000,
produce a table of the wordcounts of all the Word documents in a user
specified directory. The sequence of operations is this;

I list all the files in a directory ending .doc, and open a reference to the
Word application. The array of filenames goes into a for loop, and for each
file I get a reference to the "documents" property of Word, use the Open
method on the documents object, specifying the filename I want and
specifying that the file is to be opened read-only, not visible and not
added to the recent file list. I then use the ComputeStatistics method on
the new document to get the word count, before closing the "Close" method on
the new document object. Then
I close the reference to the "Documents"
object and, just in case, to the "Document" that I've just closed. The loop
then starts again. The reference to the Word application is closed when the
loop exits.

The program works, kind of, however there are two problems;

1) Word is including tables of contents in the word count. Automatically
generated tables should not be included in this word count since it's just
Word making a copy of text elsewhere in the document.

2) When you go into Word afterwards to do anything and then close it you are
prompted to save changes to all the files opened by the VI, even though you
haven't touched the files yourself and the VI has both opened them read only
and specified that no changes are to be saved.

Does anyone know how I could tackle these two problems? I'm not keen on
trying to retrieve the text from Word and parse it to identify the end of
the TOC...
0 Kudos
Message 1 of 5
(2,846 Views)
Craig,

Ah, the joys of Word2000 and ActiveX...

1) About the word count including the TOC, you may be able to select a range
of the document that doesn't include the TOC, and then do your
ComputeStatistics on that selection. I'm not sure how Word delineates where
the TOC begins or ends, but since it's a just a big Field object it might be
fairly easy to isolate. Alternatively, you might find it easier to select
ONLY the TOC, do your ComputeStatistics on it, and then subtract that result
from the ComputeStatistics for the entire document. That would be kind of
backwards, but it is, after all, Microsoft that we're dealing with here.
😉

2) I've had this problem myself, and I don't remember exactly how I solved
it. I do remember that it wasn't very elegant. I have a few ideas:
a) Explicitly Quit Word after you're done processing the files. This may
be too inconvenient if you've already got Word running for some other
purpose.
b) I believe you can use another ActiveX call (forgot which one exactly)
to close the files and explicitly indicate to not save changes. Perhaps
you're doing that already, though, in which case your problem is a bit of a
mystery.
c) Turn off the Office Assistant. I have become very suspicious of the
little animated helper. The dialogs that the Assistant presents do not seem
to behave quite the same as the other "regular" dialogs you see when he's
turned off, and this has caused me difficulty in the past when working with
Word in "invisible" mode.

Sorry I can't be more specific, but I hope that helps a bit!
Justin

Justin J. Goeres
Project Engineer
jgoeres@indiosystems.com
phone (925)485-5083 ; fax (435)305-8044

Indio Systems, Inc.
5600-A Sunol Blvd.
Pleasanton, CA 94588
"Craig Graham" wrote in message
news:39fd8a27@newsgroups.ni.com...
> I'm trying to cobble together a VI that will, via ActiveX and MS Word2000,
> produce a table of the wordcounts of all the Word documents in a user
> specified directory. The sequence of operations is this;
>
> I list all the files in a directory ending .doc, and open a reference to
the
> Word application. The array of filenames goes into a for loop, and for
each
> file I get a reference to the "documents" property of Word, use the Open
> method on the documents object, specifying the filename I want and
> specifying that the file is to be opened read-only, not visible and not
> added to the recent file list. I then use the ComputeStatistics method on
> the new document to get the word count, before closing the "Close" method
on
> the new document object. Then I close the reference to the "Documents"
> object and, just in case, to the "Document" that I've just closed. The
loop
> then starts again. The reference to the Word application is closed when
the
> loop exits.
>
> The program works, kind of, however there are two problems;
>
> 1) Word is including tables of contents in the word count. Automatically
> generated tables should not be included in this word count since it's just
> Word making a copy of text elsewhere in the document.
>
> 2) When you go into Word afterwards to do anything and then close it you
are
> prompted to save changes to all the files opened by the VI, even though
you
> haven't touched the files yourself and the VI has both opened them read
only
> and specified that no changes are to be saved.
>
> Does anyone know how I could tackle these two problems? I'm not keen on
> trying to retrieve the text from Word and parse it to identify the end of
> the TOC...
>
>
>
0 Kudos
Message 2 of 5
(2,846 Views)
Craig,

To add to Justin's 2) b) comment:

Right before you use 'Automation Close' sub-vi on the application reference
created for word_application, use another 'invoke node' sub-vi (on the app
ref) and select 'Quit' method. Below that you will see options, one of which
is 'SaveChanges'. This should help.

I had a similar problem. Even with the automation close vi closing the application
reference, win95 task manager would still show WORD in the task list. Run
my VI ten times, have ten WORD's in the task list. The only way was to use
the quit method before closing the reference.

Good luck, hope this helps,

Jared

"Justin Goeres" wrote:
>2) I've had this problem myself, and I don't remember exactly how I solved>it.
I do re
member that it wasn't very elegant. I have a few ideas:> b) I
believe you can use another ActiveX call (forgot which one exactly)>to close
the files and explicitly indicate to not save changes. Perhaps>you're doing
that already, though, in which case your problem is a bit of a>mystery
0 Kudos
Message 3 of 5
(2,846 Views)
Thanks to both for the comments.

I've not had time to mess about with the table of contents, but I've just
had another crack at the problem of Word hanging around. Explicitly quitting
the application works, but of course since you specify not to save, is
highly bad news if the user happens to have work open.

The way I've discovered to tackle this is by wiring a true to the "Open New
Instance" input of the "Automation Open" node. The new instance is then the
one that invisibly handles the files and then is hit by the application quit
method, without affecting any Word documents the user has open. Neat.

Jared O'Mara wrote in message
news:3a01d654@newsgroups.ni.com...
>
> Craig,
>
> To add to Justin's 2) b) comment:
>
> Right b
efore you use 'Automation Close' sub-vi on the application
reference
> created for word_application, use another 'invoke node' sub-vi (on the app
> ref) and select 'Quit' method. Below that you will see options, one of
which
> is 'SaveChanges'. This should help.
>
> I had a similar problem. Even with the automation close vi closing the
application
> reference, win95 task manager would still show WORD in the task list. Run
> my VI ten times, have ten WORD's in the task list. The only way was to
use
> the quit method before closing the reference.
>
0 Kudos
Message 4 of 5
(2,846 Views)
And for my next trick, excluding the table of contents can be achieved by
deleting it- the file won't be saved anyhow. One of the properties of the
"Document" object is a collection of all the tables of contents in the
document, and the TOC object has a "delete" method. There's also other
automatically generated tables there that can probably be similarly zapped,
but that's unnecessary for this one.

Craig Graham wrote in message
news:3a01da4f@newsgroups.ni.com...
> Thanks to both for the comments.
>
> I've not had time to mess about with the table of contents, but I've just
> had another crack at the problem of Word hanging around. Explicitly
quitting
> the application works, but of course since you specify n
ot to save, is
> highly bad news if the user happens to have work open.
0 Kudos
Message 5 of 5
(2,847 Views)