Use PowerShell to Count Comments in Word Docs


Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to count comments in Microsoft Word documents.

Microsoft Scripting Guy, Ed Wilson, is here. The process of writing often includes rewriting. In fact, I imagine that I spend more time rewriting stuff than I spent writing the original document—especially when I write a book. The book process goes something like this:

I write a chapter, I send it to my editor. She looks it over for style and consistency within the series, and then she forwards it to the technical reviewer. The technical reviewer sends it back to the editor, who forwards it to the line editor. The line editor returns it to the editor who then returns it to me. I review all of the comments, make changes, accept or suggest other changes, and the process completes another iteration. The documents finally go to the publisher, who returns page proofs, and I have one final chance for correction.

I turn in chapters on a regular basis, but sometimes chapters return to me in batches. When that happens, I like to know which chapters are going to require more work to review and correct stuff in the comments, and which chapters will require the least amount of work. Knowing this information can help me plan my work according to how much time I have available. In the past, that required opening each document, scrolling through to the end, and making a mental note of how many comments appear. Now I can use Windows PowerShell to do this for me.

Search a folder for documents and count the comments

It dawned on me that I could use Windows PowerShell to tell me which documents in a folder contain the most comments. Armed with this information, I would know where to focus my attention. It would also be useful if I had a collection of documents that I needed to review and add comments to. It would help me to know if I missed any particular documents. If a document was perfect, I could at least add a comment that says something like, “Great job. No changes needed.”

The first things I need to do are specify the folder that contains my document collection, create the Word.Application object, set the Word automation Visible property to $false, and use the Foreach command to walk through the collection. The code is shown here:

$Path = "E:\data\BookDOcs\PS3_StartHere"

$word = New-Object -comobject word.application

$word.visible = $false

Foreach($filepath in (Get-ChildItem $path -Filter *.docx -Recurse))

{

Now I need to open the document. Note that documents that are more complex than one section and range could have comments associated with them. Here, all of the comments are associated with the Document object:

$doc = $word.documents.open($filePath.FullName)

After I have the document open, I use the Comments object to retrieve the count that is associated with the document. If the count is greater than or equal to 1, I display the count and the file name, and then I close the document as shown here:

$count = $doc.Comments.count

  if( $count -ge 1) {"$count comments in $filepath"}

 $doc.close() 

 I remove the Document object before I move to the next file:

  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null

  Remove-Variable Doc }

 After I complete working with all of the documents in the folder, I clean up the Word.Application object and call garbage collection to free up memory. This script is shown here:

$word.quit()

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null

Remove-Variable Word

[gc]::collect()

[gc]::WaitForPendingFinalizers()

 The complete script is shown here:

$Path = "E:\data\BookDOcs\PS3_StartHere"

$word = New-Object -comobject word.application

$word.visible = $false

Foreach($filepath in (Get-ChildItem $path -Filter *.docx -Recurse))

{

 $doc = $word.documents.open($filePath.FullName)

 $count = $doc.Comments.count

  if( $count -ge 1) {"$count comments in $filepath"}

 $doc.close()

 

 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null

  Remove-Variable Doc }

$word.quit()

 

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null

Remove-Variable Word

[gc]::collect()

[gc]::WaitForPendingFinalizers()

When I run the script, the following output appears in the console pane:

 Image of command output

That is all there is to using Windows PowerShell to count the number of comments in a document. Join me tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Comments (0)

Skip to main content