Help with Word doc page numbers

I’m having a difficult time with retrieving the page numbers of word documents in folder directory. I have more than one word documents in a directory located in:

C:\test\docs

that contain test1.doc, test2.doc and test3.doc

I was able to retrieve the page number for just one document and export the path along with the page number. However I’m having some trouble with retrieving the page numbers for the other documents and export them to a text file. Below is my code I have so far:

[pre]
Set-Location “C:\test\docs”

$Path = “C:\test\html\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.txt”
[/pre]

The output for the code above is:
C:\test\docs\test1.doc has 1 pages

How do I retrieve the page numbers for the other documents and output it in the following format:

C:\test\docs\test1.doc has 1 pages
C:\test\docs\test2.doc has 5 pages
C:\test\docs\test3.doc has 10 pages

note: the page number depends on how much content is in the word document.

I thought you already had your answer …

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