Office Space: Tips and Tricks for Scripting Microsoft Office Applications
Welcome to Office Space, the new column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to firstname.lastname@example.org (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.
Getting Statistics for a Microsoft Word Document
One of the more useful – though oft-times neglected – features of Microsoft Word is its ability to generate statistics for a document. To get this information you simply go to the File menu, click Properties, and then click on the Statistics tab:
There you have it: pages, paragraphs, lines, words, characters – what more could you ask for?
Well, OK, maybe there is one thing: is there any way to get at this same information using a script? You bet there is.
In fact, here’s a script that opens the document C:\Scripts\Test.doc and reports back the document statistics:
Const wdStatisticCharacters = 3 Const wdStatisticCharactersWithSpaces = 5 Const wdStatisticFarEastCharacters = 6 Const wdStatisticLines = 1 Const wdStatisticPages = 2 Const wdStatisticParagraphs = 4 Const wdStatisticWords = 0 Set objWord = CreateObject("Word.Application") Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc") Wscript.Echo "Characters: " & _ objDoc.ComputeStatistics(wdStatisticCharacters) Wscript.Echo "Characters with spaces: " & _ objDoc.ComputeStatistics(wdStatisticCharactersWithSpaces) Wscript.Echo "Far East characters: " & _ objDoc.ComputeStatistics(wdStatisticFarEastCharacters) Wscript.Echo "Lines: " & objDoc.ComputeStatistics(wdStatisticLines) Wscript.Echo "Pages: " & objDoc.ComputeStatistics(wdStatisticPages) Wscript.Echo "Paragraphs: " & objDoc.ComputeStatistics(wdStatisticParagraphs) Wscript.Echo "Words: " & objDoc.ComputeStatistics(wdStatisticWords) objDoc.Saved = TRUE objWord.Quit
Ok, so it looks a little complicated, but it’s really pretty easy. We begin by defining a slew of constants. These constants represent the various statistics Word can compile for us. We then create an instance of the Word.Application object, and call the Open method in order to open the file C:\Scripts\Test.doc.
(Note that, in this script, we didn’t set the Visible property to True; that’s because there really isn’t anything for us to see. With this script, everything happens in an invisible window.)
With the document open we repeatedly call the ComputeStatistics method, each time passing the method a new constant. For example, this line of code retrieves – and displays on screen – the number of words in the document:
Wscript.Echo "Words: " & objDoc.ComputeStatistics(wdStatisticWords)
That’s it, except for one important detail. Calculating statistics is considered a change to the document; consequently when we try to quit Word we’re prompted with a “Do you want to save your changes?” dialog box. The truth is, we don’t really care about these “changes” and we don’t really want to be bothered with having to answer this dialog box. Therefore, in the next-to-last line of the script we set the value of the document’s Saved property to True. That effectively “tricks” Word into thinking we’ve already saved the changes. We can then quit Word without receiving the “Do you want to save your changes?” dialog box.