{"id":9121,"date":"2012-06-12T00:01:00","date_gmt":"2012-06-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/12\/use-powershell-to-create-a-characters-word-document\/"},"modified":"2012-06-12T00:01:00","modified_gmt":"2012-06-12T00:01:00","slug":"use-powershell-to-create-a-characters-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-a-characters-word-document\/","title":{"rendered":"Use PowerShell to Create a Character&#8217;s Word Document"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a Word document that displays all characters in a font.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. If it is Tuesday, I must still be at TechEd 2012 in Orlando, Florida. In fact, this morning at 10:30 I have an autograph session at the O&rsquo;Reilly booth. They will be handing out a few free copies of my Windows PowerShell&nbsp;2.0 Best Practices book, so get in line early if you want to ensure success for scrounging one. If not&hellip;well, you can always buy one.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp;&nbsp;&nbsp;This week I am talking about using Windows PowerShell to automate Microsoft Word. I have written extensively about this subject in the past. Today&rsquo;s blog continues working with inserting special characters in a document. Yesterday, I talked about the basic technique to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/06\/11\/add-a-symbol-to-a-word-document-by-using-powershell.aspx\" target=\"_blank\">Add a Symbol to a Word Document by Using PowerShell<\/a>.<\/p>\n<h2>Printing out all characters and codes from a font<\/h2>\n<p>When you work to automate Microsoft Word (whether you are using VBScript, Windows PowerShell, or some other language), many of the steps end up being exactly the same, no matter what you are doing. In yesterday&rsquo;s blog, I discussed each of the steps. Today I am going to skip past the steps after I list the three basic steps.<\/p>\n<p><b>Just the steps<\/b><\/p>\n<ol>\n<li>Create an instance of the Word.Application object. Store the returned object in a variable.<\/li>\n<li>Make the Word application visible.<\/li>\n<li>Add a document object to the Word application.<\/li>\n<\/ol>\n<p>You will nearly always perform these same three steps, no matter what you are doing with Word automation. The code to perform these three steps is shown here.<\/p>\n<p style=\"padding-left: 30px\">$word = New-Object -ComObject word.application<\/p>\n<p style=\"padding-left: 30px\">$word.visible = $true<\/p>\n<p style=\"padding-left: 30px\">$document = $word.documents.add()<\/p>\n<h2>Creating and using a Selection object<\/h2>\n<p>In yesterday&rsquo;s Hey, Scripting Guy! Blog, I mentioned that there are two objects that expose the <b>InsertSymbol<\/b><i> <\/i>method. Those objects are the <b>Range<\/b> object and the <b>Selection<\/b> object. Yesterday, I used the <b>Range<\/b> object, so today I will use the <b>Selection<\/b> object. For this example, it really does not matter much which object exposes the method. I guess the point of the exercise is to show you that the method behaves the same, no matter which object exposes it.<\/p>\n<p>To create a <b>Selection<\/b> object use the <b>Selection<\/b> property from the <b>Document<\/b> object, and store the returned <b>Selection<\/b> object in a variable. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">$selection = $word.Selection<\/p>\n<p>From yesterday&rsquo;s blog, we also know that the characters range from 33 through 255. This is a perfect time to use the <b>for<\/b><i> <\/i>statement because we know exactly how many items with which we will work. The following code uses the <b>for<\/b><i> <\/i>statement to control a loop that goes from 33 through 255 and increments by 1.<\/p>\n<p style=\"padding-left: 30px\">For($i = 33 ; $i -le 255; $i++)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p>Inside the <b>for<\/b><i> <\/i>loop, I use the <b>TypeText<\/b><i> <\/i>method from the <b>Selection<\/b> object to write the number that is contained in the variable <b>$i<\/b>. The <b>`t<\/b> creates a tab character in Windows PowerShell. Therefore, the <b>TypeText<\/b><i> <\/i>method displays a number and then tabs over one tab stop. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$selection.TypeText(&#8220;$i `t&#8221;)<\/p>\n<p>Now I need to use the <b>InsertSymbol<\/b><i> <\/i>method to display the character that is associated with the specific character code represented by the number stored in the <b>$i<\/b> variable. In the code that follows, I use the font &ldquo;Segoe&rdquo; to illustrate that this technique works with any installed font.<\/p>\n<p style=\"padding-left: 30px\">$selection.InsertSymbol($i,&#8221;Segoe&#8221;)<\/p>\n<p>The <b>TypeParagraph<\/b><i> <\/i>method creates an empty paragraph between each line that displays a character code and its associated symbol. Without the <b>TypeParagraph<\/b><i> <\/i>method, the text would wrap line after line, and it would be difficult to read. The code to create the space between the lines of output is shown here.<\/p>\n<p style=\"padding-left: 30px\">$selection.TypeParagraph()<\/p>\n<p>The complete CreateWordSymbolFile.ps1 script appears here.<\/p>\n<p style=\"padding-left: 30px\"><b>CreateWordSymbolFile.ps1<\/b><\/p>\n<p style=\"padding-left: 30px\">$word = New-Object -ComObject word.application<\/p>\n<p style=\"padding-left: 30px\">$word.visible = $true<\/p>\n<p style=\"padding-left: 30px\">$document = $word.documents.add()<\/p>\n<p style=\"padding-left: 30px\">$selection = $word.Selection<\/p>\n<p style=\"padding-left: 30px\">For($i = 33 ; $i -le 255; $i++)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$selection.TypeText(&#8220;$i `t&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$selection.InsertSymbol($i,&#8221;Segoe&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$selection.TypeParagraph()<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>When I run the script, the output in the image that follows appears.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7318.HSG-6-12-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7318.HSG-6-12-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Microsoft Word <span>Automation&nbsp;<\/span>Week will continue tomorrow when I will talk about creating a table in a Microsoft Word document and inserting corresponding special characters in it. It is a really cool blog, and it illustrates several useful techniques.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a Word document that displays all characters in a font. Microsoft Scripting Guy, Ed Wilson, is here. If it is Tuesday, I must still be at TechEd 2012 in Orlando, Florida. In fact, this morning at 10:30 I have an autograph [&hellip;]<\/p>\n","protected":false},"author":596,"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-9121","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>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a Word document that displays all characters in a font. Microsoft Scripting Guy, Ed Wilson, is here. If it is Tuesday, I must still be at TechEd 2012 in Orlando, Florida. In fact, this morning at 10:30 I have an autograph [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9121","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=9121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9121\/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=9121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}