{"id":66523,"date":"2006-09-07T13:24:00","date_gmt":"2006-09-07T13:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/07\/how-can-i-get-a-total-page-count-for-all-the-word-documents-in-a-folder\/"},"modified":"2006-09-07T13:24:00","modified_gmt":"2006-09-07T13:24:00","slug":"how-can-i-get-a-total-page-count-for-all-the-word-documents-in-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-total-page-count-for-all-the-word-documents-in-a-folder\/","title":{"rendered":"How Can I Get a Total Page Count for All the Word Documents in a Folder?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I get a total page count for all the Word documents in a folder?<BR><BR>&#8212; RA<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RA. You\u2019ll have to forgive the Scripting Guy who writes this column if he seems a little tired this morning. Yesterday was the first day of school in the Lake Washington School District and, as you might expect, the scene in the Scripting Household was one of utter chaos: the kicking and screaming, the tears and tantrums, the sudden onset of beriberi, which made it <I>impossible<\/I> to leave the house. All in all, it made for a very long and very tiring morning.<\/P>\n<P>Of course, now that we think about it, all the screaming and crying seemed to emanate from the Scripting Dad, who didn\u2019t want to go to work. The Scripting Son calmly got out of bed and went to school, like he always does.<\/P>\n<P>Despite the tears, and despite the beriberi, the Scripting Dad knew he had no choice but to come in to work. After all, the world (or at least you, RA) was depending on him; <I>someone<\/I> had to come up with a script that could give you a total page count for all the Word documents in a folder:<\/P><PRE class=\"codeSample\">Const wdStatisticPages = 2<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Scripts&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>For Each objFile in colFiles\n    If objFile.Extension = &#8220;doc&#8221; Then\n        Set objDoc = objWord.Documents.Open(objFile.Name)\n        intPages = intPages + objDoc.ComputeStatistics(wdStatisticPages)\n        objDoc.Saved = True\n        objDoc.Close\n    End If\nNext<\/p>\n<p>Wscript.Echo &#8220;Total pages: &#8221; &amp; intPages\nobjWord.Quit\n<\/PRE>\n<TABLE id=\"EGD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. Does the Scripting Guy who writes this column <I>really<\/I> have beriberi? Well, probably not. On the other hand, take a look at some of the symptoms of beriberi:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P class=\"lastInCell\">Apathy<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P class=\"lastInCell\">Drowsiness<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P class=\"lastInCell\">Poor mental concentration<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Quick, someone call a doctor right away!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>OK; now that the script is written let\u2019s see if we can figure out how it works. To begin with, we define a constant named wdStatisticPages and set the value to 2; we\u2019ll use this constant to retrieve the number of pages in each Word document we find.<\/P>\n<TABLE id=\"E4D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Are there <I>other<\/I> statistics we can retrieve for a Word document? You bet there are; for more information, see one of our most-beloved <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/mar05\/tips0324.mspx\"><B>Office Space<\/B><\/A> columns.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After defining the constant we connect to the WMI service on the local computer and then use this line of code to retrieve a collection of all the files found in the folder C:\\Scripts:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Scripts&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>Next we \u2013 what\u2019s that? Can you perform this same task against a <I>remote<\/I> computer? As a matter of fact you can, provided that Microsoft Word is installed on that remote computer. In order to pull off that trick you need to do two things. First, you need to assign the name of that remote machine to the variable strComputer; for example:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>That enables us to connect to the WMI service on the remote computer. <\/P>\n<P>Second, you need to create an instance of the <B>Word.Application<\/B> object on the remote machine. In the sample script we\u2019re showing you today we\u2019re working with Word documents on the local computer; therefore we use this line of code to create an instance of Word:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\n<\/PRE>\n<P>However, if you want to work with Word documents on a remote computer you need to include the computer name as the optional second parameter to the <B>CreateObject<\/B> method:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;, &#8220;atl-ws-01&#8221;)\n<\/PRE>\n<P>Got that? Good.<\/P>\n<P>OK, so where were we? Oh, yeah: we create an instance of the Word.Application object (in this case, an instance of Word on the local computer) and then set the <B>Visible<\/B> property to True. That, by the way, is optional: we don\u2019t need Word to be visible onscreen in order to add up the number of pages in the various Word documents. However, this <I>does<\/I> give a visual cue that something is going on. Leave that line of code in or take it out; that\u2019s entirely up to you.<\/P>\n<P>Now it\u2019s time to get serious. To begin with we set up a For Each loop to loop through the collection of files found in the folder C:\\Scripts. The first thing we do inside that loop is check to see whether we\u2019re working with a Word document (that is, a file with a <B>.doc<\/B> file extension):<\/P><PRE class=\"codeSample\">If objFile.Extension = &#8220;doc&#8221; Then\n<\/PRE>\n<TABLE id=\"EVF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, thanks for pointing that out: do <I>not<\/I> include the period when specifying a file extension in WMI. It\u2019s <I>doc<\/I>, not <I>.doc<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s say that we do have a Word document. What then? Well, for starters, we use this line of code to open the document:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Open(objFile.Name)\n<\/PRE>\n<P>We can do this because the <B>Name<\/B> property gives us the complete path to the file. With the document open we can then use the <B>ComputeStatistics<\/B> method to determine the number of pages in the file:<\/P><PRE class=\"codeSample\">intPages = intPages + objDoc.ComputeStatistics(wdStatisticPages)\n<\/PRE>\n<P>As you can see, we\u2019re using a variable named intPages to keep track of the total number of pages. What we\u2019re doing here is determining the number of pages in our first document, something we do by calling ComputeStatistics and passing it the constant wdStatisticPages. We take the number of pages in document 1, add that to the current value of intPages, and then assign the sum of those two numbers to intPages.<\/P>\n<P>Why? Well, the first time through the loop intPages will be equal to 0; that\u2019s because we haven\u2019t assigned it a value yet. Let\u2019s assume that document 1 has 5 pages. That means our equation will look like this:<\/P><PRE class=\"codeSample\">intPages = 0 + 5\n<\/PRE>\n<P>That makes intPages equal to 5. Now, suppose we loop around and find a second Word document, this one containing 13 pages. That means our next step is to add those 13 pages to the current value of intPages (5). In other words:<\/P><PRE class=\"codeSample\">intPages = 5 + 13\n<\/PRE>\n<P>The variable intPages will now be equal to 18 which \u2013 magically enough \u2013 also equals the total number of pages we\u2019ve found so far. Clever, huh?<\/P>\n<P>Well, OK: <I>kind of<\/I> clever.<\/P>\n<P>After we get the total number of pages for the first Word document we set the <B>Saved<\/B> property to True, which ensures that Word won\u2019t ask us if we want to save changes to the document; we then close document 1. After that we loop around and repeat this process with the next file in the collection.<\/P>\n<P>When we\u2019re all done we use these two lines of code to echo back the total number of pages found and to terminate our instance of Microsoft Word:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Total pages: &#8221; &amp; intPages\nobjWord.Quit\n<\/PRE>\n<P>That should do the trick.<\/P>\n<P>You know, that\u2019s a good point: beriberi <I>does<\/I> sound pretty serious, doesn\u2019t it? So then why did the Scripting Dad come in to work? To be honest, he had at least 200 reasons: $70 for the athletic participation fee; $40 for an ASB card; $40 for a yearbook; $50 for a parking pass.<\/P>\n<P>Etc.<\/P>\n<P>Needless to say, we\u2019ll see you tomorrow.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a total page count for all the Word documents in a folder?&#8212; RA Hey, RA. You\u2019ll have to forgive the Scripting Guy who writes this column if he seems a little tired this morning. Yesterday was the first day of school in the Lake Washington School District and, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[715,38,84,49,3,12,5,395],"class_list":["post-66523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-associators-of","tag-files","tag-microsoft-word","tag-office","tag-scripting-guy","tag-storage","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get a total page count for all the Word documents in a folder?&#8212; RA Hey, RA. You\u2019ll have to forgive the Scripting Guy who writes this column if he seems a little tired this morning. Yesterday was the first day of school in the Lake Washington School District and, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66523\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}