June 23rd, 2010

Hey, Scripting Guy! Can I Use Windows PowerShell 2.0 to Print a List of Keyboard Shortcuts for Microsoft Word?


Bookmark and Share
  Hey, Scripting Guy! Question Hey, Scripting Guy! We are getting ready to deploy Office 2010 to our user community, and I need to use Windows PowerShell 2.0 to print a list of keyboard shortcuts for working with Microsoft Word. In the past, I was able to print a chart of all the keyboard shortcuts for working with Microsoft Word and provide it to our user community. However, when I tried to find help about the keyboard shortcuts, there is not a centralized list. The keyboard shortcuts are all broken up by category. Though that is fine for browsing, it is horrible for printing. Is there a way you can write a script that will put the pages back together for me? — BC   Hey, Scripting Guy! Answer Hello BC, Microsoft Scripting Guy Ed Wilson here. I had a really interesting dinner last night. A Microsoft Premier Field Engineer (PFE) was in Charlotte conducting a Train-the-Trainer session for a new Windows PowerShell workshop they are developing. He sent email to ask if the Scripting Wife and I would like to join him for dinner. Of course, we said yes. As it turned out, he had invited one of his students to join him, and that student was a SharePoint guru. Well, needless to say, I feel sorry for the poor old Scripting Wife. The SharePoint guru and I kind of got carried away with our discussion. I was actually trying to recruit him to contribute a guest blog post or two for the Hey, Scripting Guy! Blog. One of the cool things about SharePoint 2010 is the plethora of new Windows PowerShell cmdlets. I have spoken to a couple of SharePoint user groups recently, and it seems there is quite a bit of interest in learning Windows PowerShell in that community. When we got home, the Scripting Wife was a little annoyed that I did not include her in our conversation, so I thought it might be a decent time to catch up on some of the scripter@microsoft.com email. I decided to write the PrintWordShortCuts.ps1 script to illustrate using the ListCommands method from the Word.Application object. The complete PrintWordShortCuts.ps1 script is shown here. PrintWordShortCuts.ps1 [ref]$SaveOption = “microsoft.office.interop.word.WdSaveOptions” -as [type]
$application = New-Object -ComObject word.application
$application.visible = $true
$application.ListCommands($false)
$doc = $application.activeDocument
$doc.printout()
$doc.Close([ref]$saveOption::wdDoNotSaveChanges)
$application.Quit()
Remove-Variable -name application
[gc]::collect()
[gc]::WaitForPendingFinalizers()
The first thing to do is to create the WdSaveOptions enumeration type. This will be used to tell Microsoft Word how to save the document, but in this script we will not save the document that gets created. The WdSaveOptions enumeration must be passed as a reference and therefore the [ref] type constraint is used. This line of code is shown here: [ref]$SaveOption = “microsoft.office.interop.word.WdSaveOptions” -as [type] The Word.Application object is created; it is always created when working with Microsoft Word. In this script, we are setting its visible property to $true because we are going to create a document, print it, and then discard the document. This section of the script is shown here: $application = New-Object -ComObject word.application
$application.visible = $true
The ListCommands method from the application object accepts a Boolean value—either $true or $falseas a mandatory parameter. When the ListAllCommands parameter is set to $false, only Microsoft Word commands that have a shortcut associated with them will be printed. If the ListAllCommands parameter is set to $true, all commands are printed. This line of code is shown here: $application.ListCommands($false) Unfortunately, the ListCommands method from the application object does not return a document object. But the ActiveDocument property from the application object will return a document object. Store the returned document object in the $doc variable, as shown here: $doc = $application.activeDocument The printout method from the document object has a large number of parameters that can be specified. Luckily, you can simply call the method and send the document to the default printer. My default printer is set to automatically perform duplex printing because I like to save paper; therefore, there was nothing I really wanted to configure for the print operation in this script. The method call is shown here: $doc.printout() The document shown in the following image appears as the print operation takes place. Image of document that appears as print operation takes place It is time to close the document. To discard the document without having to respond to a prompt, use the wdDoNotSaveChanges enum value from the WdSaveOptions enumeration. This value needs to be passed by reference to the close method of the document object. This is shown here: $doc.Close([ref]$saveOption::wdDoNotSaveChanges) To perform cleanup, you need to call the quit method of the application object. You can also remove the variable that contains the application object, and then call garbage collection. This is shown here: $application.Quit()
Remove-Variable -name application
[gc]::collect()
[gc]::WaitForPendingFinalizers()

BC, that is all there is to using Microsoft Word to print out a list of command shortcuts. Microsoft Office Week will continue tomorrow. If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson and Craig Liebendorfer, Scripting Guys

Author

0 comments

Discussion are closed.