Share via


How do I count the number of pages in each word document in Powershell?

Question

Saturday, February 16, 2019 11:08 AM

I was able to output the page numbers for just one document. How do I output the page numbers for the other documents and output the results to a text file. Below is my code:

$Path = "C:\test\docs\test1.doc" 

$word = New-Object -ComObject Word.Application $word.Visible = $true $binding = 'System.Reflection.BindingFlags' -as [type] 

$doc = $word.Documents.Open($Path) $doc.Repaginate() $prop = 

$doc.BuiltInDocumentProperties(14) 

$pages = [System.__ComObject].invokemember('value',$binding::GetProperty,$null,$prop,$null) $doc.Close(0) $word.Quit() 

$test = "$Path has $Pages pages." $test | Out-File -filepath "C:\test\docs\test.csv"

The output for the above code is:

C:\test\docs\test1.doc has 1 pages

Note: page numbers depends on the word document.

Just not sure how to do this for the other documents. 

All replies (8)

Saturday, February 16, 2019 12:00 PM ✅Answered

This is the correct way to get total pages and the correct way to format code.

$path = 'd:\testps1\test1.doc'

$word = New-Object -ComObject Word.Application 
$word.Visible = $true 
$doc = $word.Documents.Open($path)
$pages = $word.ActiveDocument.ComputeStatistics([Microsoft.Office.Interop.Word.WdStatistic]::wdStatisticPages)
"$path has $pages pages." | Out-File C:\test\docs\test.txt

$doc.Close(0) 
$word.Quit()

\(ツ)_/


Saturday, February 16, 2019 11:36 AM

There is really no answer to your question.  Page numbers ae a function of the Word program.  Other document type would not have page numbers. 

You code is also badly formatted and posted and is broken in more than one place.  If you post code be sure to post it correctly.

\(ツ)_/


Saturday, February 16, 2019 12:17 PM

My original question was how does this work if I have more than one word document? I know it works for one document, but I'm stuck of exporting the output for more than one word docs in a directory


Saturday, February 16, 2019 12:29 PM

help get-childitem-online
help ForEach-Object -online

I think you should start by learning basic PowerShell.  The above help is the fist thing all PowerShell users must learn.  All good tutorials and books teach this in the first two or three chapters.

Start here: Microsoft Virtual   Academy - Getting Started with Microsoft PowerShell

 

\(ツ)_/


Monday, February 18, 2019 2:32 AM

I was able to figure it out!


Monday, February 18, 2019 9:15 AM

Hi,

Was your issue resolved?

If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

If no, please reply and tell us the current situation in order to provide further help.

Best Regards,

Lee

Just do it.


Monday, February 25, 2019 9:59 PM

answer was not resolved by anyone on this forum! I created a for loop for more than one document in a directory and was able to append everything to a text file.


Monday, February 25, 2019 10:07 PM

The reply I posted is the only way to count Word pages that works.  If you export to a text file then you are not counting "Word" pages but are counting text lines.

\(ツ)_/