{"id":52263,"date":"2009-10-13T00:01:00","date_gmt":"2009-10-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/10\/13\/hey-scripting-guy-how-do-i-count-the-number-of-pages-in-a-group-of-office-word-documents\/"},"modified":"2009-10-13T00:01:00","modified_gmt":"2009-10-13T00:01:00","slug":"hey-scripting-guy-how-do-i-count-the-number-of-pages-in-a-group-of-office-word-documents","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-do-i-count-the-number-of-pages-in-a-group-of-office-word-documents\/","title":{"rendered":"Hey, Scripting Guy! How Do I Count the Number of Pages in a Group of Office Word Documents?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>  <\/p>\n<p><img decoding=\"async\" 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\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I am so mad right now, I could just about scream. In fact, if you don&#8217;t mind, I believe I will scream. We have this stupid printer at work that I have to use to print out a very important report. This printer is rather slow when printing out these reports; it prints out about 30 pages a minute. It has a hopper that holds 10 reams of paper, which is 5,000 sheets. <\/p>\n<p class=\"MsoNormal\">Why am I telling you all this? Well, when I print out the monthly report, it takes several hours to print. I generally go and fill up the hopper with all the paper it will hold, go back to my desk, and start the print job and go home. When I come back the next day, the reports are ready<span>&mdash;<\/span> if I am lucky. Tying up the printer during the day is not a real option, and I really do not want to have to stay late and babysit the machine. Why am I mad, you may ask? The last two months in a row the stupid printer has run out of paper. It seems that some of the partners have been adding extra stuff to their monthly reports (probably trying to explain their poor performance, but that is a different story). Is there a script I can use that will tell me the page count of all the Microsoft Word documents in a folder? I could then run that script and tell if I needed to reduce the number of copies of the report I was making. <\/p>\n<p class=\"MsoNormal\">&#8212; KR<\/p>\n<p><img decoding=\"async\" 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\"><\/p>\n<p class=\"MsoNormal\">Hello KR, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. Well the sun is shining here in Charlotte, NC in the United States. For the last couple of days, when I woke up to go running, I thought I was back in Edinburgh, Scotland. It may have been the cool thick morning air, or maybe it is the time of year. The last time I was in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Edinburgh\"><font face=\"Segoe\">Edinburgh<\/font><\/a>, it was during <a href=\"http:\/\/en.wikipedia.org\/wiki\/Halloween\"><font face=\"Segoe\">Halloween<\/font><\/a> (which is a cool time to visit that city by the way). Anyway, KR, I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Derek_Trucks\"><font face=\"Segoe\">Derek Trucks<\/font><\/a> on my Zune, thinking about the last time I was in Edinburgh, and looking through your e-mail to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><span>scripter@microsoft.com<\/span><\/a>. I was writing the Microsoft Press book, <a href=\"http:\/\/www.amazon.com\/gp\/product\/0735623953?ie=UTF8&amp;tag=tn-script-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735623953\"><font face=\"Segoe\">Windows PowerShell Step by Step<\/font><\/a>, during my trip to Edinburgh and was reminded of the book-writing process by your e-mail. One of the things a writer is always concerned about is the number of pages that will be in the final document. <\/p>\n<p class=\"MsoNormal\">KR, I decided to write a Windows PowerShell script for you that will list the number of pages for each Word document in a folder. The complete GetPageCountOfWordDocs.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>GetPageCountOfWordDocs.ps1<\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">Function Set-Variables<br>{<br><span>&nbsp;<\/span>$folderpath = &#8220;c:fso*&#8221;<br><span>&nbsp;<\/span>$fileTypes = &#8220;*.docx&#8221;,&#8221;*doc&#8221;<br><span>&nbsp;<\/span>$confirmConversion = $false<br><span>&nbsp;<\/span>$readOnly = $true<br><span>&nbsp;<\/span>$addToRecent = $false<br><span>&nbsp;<\/span>$passwordDocument = &#8220;password&#8221;<br><span>&nbsp;<\/span>$pageCountFile = &#8220;C:fsoPageCount.csv&#8221;<br><span>&nbsp;<\/span>$numberOfPages = 0<br><span>&nbsp;<\/span>Set-OutputFile<br>} #end Set-Variables<\/p>\n<p>Function Set-OutputFile<br>{<br><span>&nbsp;<\/span>if(Test-Path -path $pageCountFile)<br><span>&nbsp;&nbsp; <\/span>{ Remove-Item -path $pageCountFile }<br><span>&nbsp;<\/span>&#8220;name,pageCount&#8221; &gt;&gt; $pageCountFile<br><span>&nbsp;<\/span>Get-WordDocuments<br>} #end Set-OutputFile<\/p>\n<p>Function Get-WordDocuments<br>{<br><span>&nbsp; <\/span>&#8220;Counting pages in Word Docs in $folderPath&#8221;<br><span>&nbsp;<\/span>$word = New-Object -ComObject word.application<br><span>&nbsp;<\/span>$word.visible = $false<br><span>&nbsp;<\/span>Get-ChildItem -path $folderpath -include $fileTypes |<br><span>&nbsp;<\/span>foreach-object `<br><span>&nbsp; <\/span>{<br><span>&nbsp;&nbsp; <\/span>$path =<span>&nbsp; <\/span>($_.fullname).substring(0,($_.FullName).lastindexOf(&#8220;.&#8221;))<br><span>&nbsp;&nbsp; <\/span>$doc = $word.documents.open($_.fullname, $confirmConversion, $readOnly, <br><span>&nbsp;&nbsp; <\/span>$addToRecent,<span>&nbsp;&nbsp; <\/span>$passwordDocument)<br><span>&nbsp;&nbsp; <\/span>$window = $doc.ActiveWindow<br><span>&nbsp;&nbsp; <\/span>$panes = $window.Panes<br><span>&nbsp;&nbsp; <\/span>$pane = $Panes.item(1)<br><span>&nbsp;&nbsp; <\/span>&#8220;<span>&nbsp; <\/span>$($_.name), $($pane.pages.count)&#8221;<span>&nbsp; <\/span>&gt;&gt; $pageCountFile<br><span>&nbsp;&nbsp; <\/span>$doc.close()<br><span>&nbsp; <\/span>} #end Foreach-Object<br><span>&nbsp;<\/span>$word.Quit()<br><span>&nbsp;<\/span>Get-pageCount<br>} #end Get-WordDocuments<\/p>\n<p>Function Get-pageCount<br>{<br><span>&nbsp;<\/span>$wdcsv = import-csv -path $pageCountFile<br><span>&nbsp;<\/span>for ($i = 0 ; $i -le $wdcsv.length -1 ; $i++)<br><span>&nbsp;<\/span>{<br><span>&nbsp; <\/span>$numberOfPages += [int32]$wdcsv[$i].pageCount<br><span>&nbsp;<\/span>}<br><span>&nbsp;<\/span>$numberOfPages<br>} #end Get-pageCount<\/p>\n<p># *** Entry Point to Script ***<\/p>\n<p>Set-Variables<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p>&lt;<\/p>\n<p>p style=&#8221;MARGIN: 0in 0in 8pt&#8221; class=&#8221;MsoNormal&#8221;&gt;In Microsoft Office Word 2007 and in Microsoft Word 2010, the number of pages in a document is displayed in the lower left corner of the document. The information can also be obtained from the review tab by clicking the Word Count button.<\/p>\n<p>The Word Count dialog box is shown here:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Word Count dialog box\" alt=\"Image of Word Count dialog box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1013\/hsg-10-13-09-01.jpg\" width=\"248\" height=\"210\"><\/p>\n<p class=\"MsoNormal\">But, KR, as you pointed out, if you have several Microsoft Word documents you will need to script the page count total. The GetPageCountOfWordDocs.ps1 script is based upon the CountWordsInWord.ps1 from <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/10\/12\/hey-scripting-guy-october-12-2009.aspx\"><span>yesterday&rsquo;s Hey<\/span>,<span> Scripting Guy! <\/span>post<\/a>. <\/p>\n<p class=\"MsoNormal\">Because the GetPageCountOfWordDocs.ps1 script performs the same basic steps, the same functions will be used. The first function is the <b>Set-Variables<\/b> function. The only changes to the function were to rename a couple of variables. The first new variable name is <b>$pageCountFile<\/b>, which contains the path to the file that will be created to hold the page counts of each Microsoft Word document. The second new variable name is the <b>$numberOfPages<\/b> variable that is used to hold the number of pages in the Word document. The <b>Set-Variables<\/b> function is shown here. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Function Set-Variables<br>{<br><span>&nbsp;<\/span>$folderpath = &#8220;c:fso*&#8221;<br><span>&nbsp;<\/span>$fileTypes = &#8220;*.docx&#8221;,&#8221;*doc&#8221;<br><span>&nbsp;<\/span>$confirmConversion = $false<br><span>&nbsp;<\/span>$readOnly = $true<br><span>&nbsp;<\/span>$addToRecent = $false<br><span>&nbsp;<\/span>$passwordDocument = &#8220;password&#8221;<br><span>&nbsp;<\/span>$pageCountFile = &#8220;C:fsoPageCount.csv&#8221;<br><span>&nbsp;<\/span>$numberOfPages = 0<br><span>&nbsp;<\/span>Set-OutputFile<br>} #end Set-Variables<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>Set-OutputFile<\/b> function is the same as the <b>Set-OutputFile<\/b> function used in the GetPageCountOfWordDocs.ps1 script. The only changes were changing variable names and the header names for the CSV file. The variable <b>$pageCountFile<\/b> holds the path to the CSV file that gets created to hold the page count data. The column header values for the output CSV file are <b>name <\/b>and <b>pageCount<\/b>. The <b>Set-OutputFile<\/b> function is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Function Set-OutputFile<br>{<br><span>&nbsp;<\/span>if(Test-Path -path $pageCountFile)<br><span>&nbsp;&nbsp; <\/span>{ Remove-Item -path $pageCountFile }<br><span>&nbsp;<\/span>&#8220;name,pageCount&#8221; &gt;&gt; $pageCountFile<br><span>&nbsp;<\/span>Get-WordDocuments<br>} #end Set-OutputFile<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>Get-WordDocuments<\/b> function begins by creating the <b>word.application<\/b> COM object. This object was discussed in yesterday&rsquo;s Hey Scripting Guy! post as well. The <b>Get-ChildItem<\/b> cmdlet returns a collection of the Microsoft Word documents from the folder, and pipes the resulting objects to the <b>ForEach-Object<\/b> cmdlet. Each document is opened by using the <b>open<\/b> method from the <b>document<\/b> object. All this was also discussed in yesterday&rsquo;s Hey Scripting Guy! post. <\/p>\n<p class=\"MsoNormal\">The part of the <b>Get-WordDocuments<\/b> function that is changed is the code that retrieves the page count. The first thing you need to do is to retrieve a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee426808.aspx\"><font face=\"Segoe\">window<\/font><\/a> object. You can obtain a b object by accessing the <b>ActiveWindow<\/b> property of the <b>document<\/b> object. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$window = $doc.ActiveWindow<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have a <b>window<\/b> object, you retrieve <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb178824.aspx\"><font face=\"Segoe\">a panes collection<\/font><\/a> by accessing the <b>panes<\/b> property of the <b>window<\/b> object. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$panes = $window.Panes<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You use the <b>item<\/b> method from the <b>panes<\/b> collection to retrieve a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb259491.aspx\"><font face=\"Segoe\">pane<\/font><\/a> object. This code is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$pane = $Panes.item(1)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have a <b>pane<\/b> object, you use the <b>pages<\/b> property to retrieve a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb179143.aspx\"><font face=\"Segoe\">pages collection<\/font><\/a>. The <b>pages<\/b> collection has the <b>count<\/b> property that will tell you how many pages are in the document. These commands are gathered together and the page count is piped to the CSV file. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">&#8220;<span>&nbsp; <\/span>$($_.name), $($pane.pages.count)&#8221;<span>&nbsp; <\/span>&gt;&gt; $pageCountFile<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">The complete Get-WordDocuments function is seen here. <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Function Get-WordDocuments<br>{<br><span>&nbsp; <\/span>&#8220;Counting pages in Word Docs in $folderPath&#8221;<br><span>&nbsp;<\/span>$word = New-Object -ComObject word.application<br><span>&nbsp;<\/span>$word.visible = $false<br><span>&nbsp;<\/span>Get-ChildItem -path $folderpath -include $fileTypes |<br><span>&nbsp;<\/span>foreach-object `<br><span>&nbsp; <\/span>{<br><span>&nbsp;&nbsp; <\/span>$path =<span>&nbsp; <\/span>($_.fullname).substring(0,($_.FullName).lastindexOf(&#8220;.&#8221;))<br><span>&nbsp;&nbsp; <\/span>$doc = $word.documents.open($_.fullname, $confirmConversion, $readOnly, <br><span>&nbsp;&nbsp; <\/span>$addToRecent,<span>&nbsp;&nbsp; <\/span>$passwordDocument)<br><span>&nbsp;&nbsp; <\/span>$window = $doc.ActiveWindow<br><span>&nbsp;&nbsp; <\/span>$panes = $window.Panes<br><span>&nbsp;&nbsp; <\/span>$pane = $Panes.item(1)<br><span>&nbsp;&nbsp; <\/span>&#8220;<span>&nbsp; <\/span>$($_.name), $($pane.pages.count)&#8221;<span>&nbsp; <\/span>&gt;&gt; $pageCountFile<br><span>&nbsp;&nbsp; <\/span>$doc.close()<br><span>&nbsp; <\/span>} #end Foreach-Object<br><span>&nbsp;<\/span>$word.Quit()<br><span>&nbsp;<\/span>Get-pageCount<br>} #end Get-WordDocuments<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>Get-pageCount<\/b> function is basically the same as the <b>Get-wordCount<\/b> function from yesterday&rsquo;s Hey, Scripting Guy! post. The only change is the variable names. The <b>$pageCountFile<\/b> variable refers to the CSV file that was created in the <b>Get-WordDocuments<\/b> function. The <b>$numberOfPages<\/b> variable is used to collect the total number of pages in the collection of Word documents. The <b>Get-pageCount<\/b> function is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Function Get-pageCount<br>{<br><span>&nbsp;<\/span>$wdcsv = import-csv -path $pageCountFile<br><span>&nbsp;<\/span>for ($i = 0 ; $i -le $wdcsv.length -1 ; $i++)<br><span>&nbsp;<\/span>{<br><span>&nbsp; <\/span>$numberOfPages += [int32]$wdcsv[$i].pageCount<br><span>&nbsp;<\/span>}<br><span>&nbsp;<\/span>$numberOfPages<br>} #end Get-pageCount<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Well, KR, thanks for a cool question. As you can see, the technique for obtaining the page count from a collection of Word documents is similar to the technique for gathering the number of words. Unfortunately, it is not as straight forward as querying the value of a different property. Join us tomorrow as Word Week continues. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/www.twitter.com\/scriptingguys\/\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/www.facebook.com\/group.php?gid=5901799452\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I am so mad right now, I could just about scream. In fact, if you don&#8217;t mind, I believe I will scream. We have this stupid printer at work that I have to use to print out a very important report. This printer is rather slow when printing out these reports; it [&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":[84,49,3,45],"class_list":["post-52263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I am so mad right now, I could just about scream. In fact, if you don&#8217;t mind, I believe I will scream. We have this stupid printer at work that I have to use to print out a very important report. This printer is rather slow when printing out these reports; it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52263","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=52263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52263\/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=52263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}