Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Use PowerShell to Count Comments in Word Docs

$
0
0

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 re-writing. In fact, I would imagine that I spend more time re-writing 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 with 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 then 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 to 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 up 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 had 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 thing I need to do is to 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. This is shown here:

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

 After I have the document opened, 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. I then close the document, as shown in this code:

$count = $doc.Comments.count

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

 $doc.close() 

 I now remove the Document object before I move on to the next file. This code is shown here:

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

  Remove-Variable Doc }

 After I have completed 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 code 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

 

 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>