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

  • Question

  • 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. 

    • Edited by j1osu2002 Monday, February 18, 2019 2:26 AM re edit
    Saturday, February 16, 2019 11:08 AM

Answers

  • 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()



    \_(ツ)_/



    • Edited by jrv Saturday, February 16, 2019 12:07 PM
    • Proposed as answer by .Christian Monday, February 18, 2019 11:08 AM
    • Marked as answer by jrv Monday, February 25, 2019 10:08 PM
    Saturday, February 16, 2019 12:00 PM

All replies

  • 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 11:36 AM
  • 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()



    \_(ツ)_/



    • Edited by jrv Saturday, February 16, 2019 12:07 PM
    • Proposed as answer by .Christian Monday, February 18, 2019 11:08 AM
    • Marked as answer by jrv Monday, February 25, 2019 10:08 PM
    Saturday, February 16, 2019 12:00 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:17 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

     


    \_(ツ)_/

    Saturday, February 16, 2019 12:29 PM
  • I was able to figure it out!
    Monday, February 18, 2019 2:32 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 18, 2019 9:15 AM
  • 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.
    • Marked as answer by j1osu2002 Monday, February 25, 2019 10:00 PM
    • Edited by j1osu2002 Monday, February 25, 2019 10:02 PM
    • Unmarked as answer by jrv Monday, February 25, 2019 10:08 PM
    Monday, February 25, 2019 9:59 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.


    \_(ツ)_/

    Monday, February 25, 2019 10:07 PM