{"id":17971,"date":"2010-06-23T00:01:00","date_gmt":"2010-06-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/06\/23\/hey-scripting-guy-can-i-use-windows-powershell-2-0-to-print-a-list-of-keyboard-shortcuts-for-microsoft-word\/"},"modified":"2010-06-23T00:01:00","modified_gmt":"2010-06-23T00:01:00","slug":"hey-scripting-guy-can-i-use-windows-powershell-2-0-to-print-a-list-of-keyboard-shortcuts-for-microsoft-word","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-windows-powershell-2-0-to-print-a-list-of-keyboard-shortcuts-for-microsoft-word\/","title":{"rendered":"Hey, Scripting Guy! Can I Use Windows PowerShell 2.0 to Print a List of Keyboard Shortcuts for Microsoft Word?"},"content":{"rendered":"<p><a href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><br \/><img decoding=\"async\" height=\"16\" width=\"125\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" alt=\"Bookmark and Share\" border=\"0\"><\/a>\n&nbsp;\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\">\nHey, 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?\n&#8212; BC\n&nbsp;\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\">\nHello BC,\nMicrosoft 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.\nAs 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.\nI decided to write the PrintWordShortCuts.ps1 script to illustrate using the <b>ListCommands<\/b> method from the <b>Word.Application<\/b> object. The complete PrintWordShortCuts.ps1 script is shown here.\n<strong>PrintWordShortCuts.ps1<\/strong>\n<span style=\"background-color: #dadada\">[ref]$SaveOption = &#8220;microsoft.office.interop.word.WdSaveOptions&#8221; -as [type] <br \/>$application = New-Object -ComObject word.application <br \/>$application.visible = $true <br \/>$application.ListCommands($false) <br \/>$doc = $application.activeDocument <br \/>$doc.printout() <br \/>$doc.Close([ref]$saveOption::wdDoNotSaveChanges) <br \/>$application.Quit() <br \/>Remove-Variable -name application <br \/>[gc]::collect() <br \/>[gc]::WaitForPendingFinalizers()<\/span>\nThe first thing to do is to create the <b>WdSaveOptions<\/b> 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 <b>WdSaveOptions<\/b> enumeration must be passed as a reference and therefore the <b>[ref]<\/b> type constraint is used. This line of code is shown here:\n<span style=\"background-color: #dadada\">[ref]$SaveOption = &#8220;microsoft.office.interop.word.WdSaveOptions&#8221; -as [type]<\/span>\nThe <b>Word.Application<\/b> object is created; it is always created when working with Microsoft Word. In this script, we are setting its visible property to <b>$true<\/b> because we are going to create a document, print it, and then discard the document. This section of the script is shown here:\n<span style=\"background-color: #dadada\">$application = New-Object -ComObject word.application <br \/>$application.visible = $true<\/span>\nThe <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb221396(office.12).aspx\">ListCommands method<\/a> from the <b>application<\/b> object accepts a Boolean value&mdash;either <b>$true<\/b> or <b>$false<\/b><b>&mdash;<\/b>as a mandatory parameter. When the <b>ListAllCommands<\/b> parameter is set to <b>$false<\/b>, only Microsoft Word commands that have a shortcut associated with them will be printed. If the <b>ListAllCommands<\/b> parameter is set to <b>$true<\/b>, all commands are printed. This line of code is shown here:\n<span style=\"background-color: #dadada\">$application.ListCommands($false)<\/span>\nUnfortunately, the <b>ListCommands<\/b> method from the <b>application<\/b> object does not return a <b>document<\/b> object. But the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb179183(v=office.12).aspx\">ActiveDocument property<\/a> from the <b>application<\/b> object will return a <b>document<\/b> object. Store the returned <b>document<\/b> object in the <b>$doc<\/b> variable, as shown here:\n<span style=\"background-color: #dadada\">$doc = $application.activeDocument<\/span>\nThe <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb237242(v=office.12).aspx\">printout method<\/a> from the <b>document<\/b> 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:\n<span style=\"background-color: #dadada\">$doc.printout()<\/span>\nThe document shown in the following image appears as the print operation takes place.\n<img decoding=\"async\" height=\"449\" width=\"600\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0623\/hsg-06-23-10-01.jpg\" alt=\"Image of document that appears as print operation takes place\" style=\"max-width: 600px;border: 0px\">\nIt is time to close the document. To discard the document without having to respond to a prompt, use the <b>wdDoNotSaveChanges<\/b> <b>enum<\/b> value from the <b>WdSaveOptions<\/b> enumeration. This value needs to be passed by reference to the <b>close<\/b> method of the <b>document<\/b> object. This is shown here:\n<span style=\"background-color: #dadada\">$doc.Close([ref]$saveOption::wdDoNotSaveChanges)<\/span>\nTo perform cleanup, you need to call the <b>quit<\/b> method of the <b>application<\/b> object. You can also remove the variable that contains the <b>application<\/b> object, and then call garbage collection. This is shown here:\n<span style=\"background-color: #dadada\">$application.Quit() <br \/>Remove-Variable -name application <br \/>[gc]::collect() <br \/>[gc]::WaitForPendingFinalizers()<\/span>\n<br \/>BC, that is all there is to using Microsoft Word to print out a list of command shortcuts. Microsoft Office Week will continue tomorrow.\nIf you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n<p><strong><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,45],"class_list":["post-17971","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17971"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17971\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}