{"id":51703,"date":"2009-12-28T00:01:00","date_gmt":"2009-12-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/12\/28\/hey-scripting-guy-how-can-i-italicize-specific-words-in-a-microsoft-word-document\/"},"modified":"2009-12-28T00:01:00","modified_gmt":"2009-12-28T00:01:00","slug":"hey-scripting-guy-how-can-i-italicize-specific-words-in-a-microsoft-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-italicize-specific-words-in-a-microsoft-word-document\/","title":{"rendered":"Hey, Scripting Guy! How Can I Italicize Specific Words in a Microsoft Word Document?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><font size=\"2\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I do not know about you, but I <i>hate<\/i> having to italicize certain words. Actually, I hate italicizing all words because I do not like removing my hands from the keyboard, grabbing the mouse, and then searching for where the text formatting menus have moved in Office 2007. If I could simply type my Microsoft Word document straight, and then have a script italicize the words that my company style guide requires to be italicized, it would be so sweet. Such a script would save me at least an hour a week because the pointy-headed boss is ridiculously obsessed with the company style guide, and I am convinced he never reads my first reports. Instead, he just sends them back to me to be corrected. Help!<\/p>\n<p class=\"MsoNormal\">&#8212; FM<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello FM, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I have been settling back into the swing of things after having been on holiday for a couple of days&mdash;it is amazing how quickly e-mail can pile up. I had a great meeting with the Scripting Manager this afternoon. We were talking about the Weekend Scripter series that we will start in February 2010. That is right; the Scripting Guys will be 7 days a week, 365 days a year! You will never be without the Scripting Guys. For example, while you are sitting around on New Year&rsquo;s Day, you can pop over to our site and read the latest Hey, Scripting Guy! Blog post (it will be a Quick-Hits Friday article by the way). In February, you will also be able to read our articles with your Saturday morning coffee, or your Sunday afternoon cup of tea (I generally prefer a nice cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Earl_Gray_tea\"><font face=\"Segoe\">Earl Grey tea<\/font><\/a> in the afternoon, but it is entirely your choice).<\/p>\n<p class=\"MsoNormal\">FM, it&rsquo;s also your choice to italicize your company&rsquo;s style words while typing or wait until you have completed writing your document and run a script. That is right: I wrote the ItalicWordsInWord.ps1 script to give you the freedom to clean up your document after you have written it. The complete ItalicWordsInWord.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>ItalicWordsInWord.ps1<\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$application = New-Object -comobject word.application<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$application.visible = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$document = $application.documents.open(&#8220;C:fsoTest.docx&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$selection = $application.Selection<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$words = &#8220;exchange&#8221;,&#8221;sql&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchCase = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchWholeWord = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchWildCards = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchSoundsLike = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchAllWordForms = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$forward = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$wrap = 1<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$format = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$replace = 2<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">Foreach ($word in $words)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$findText = $word<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$replaceWith = $word<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$selection.find.replacement.font.italic = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$exeRTN = $selection.find.execute($findText,$matchCase,<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$matchWholeWord,$matchWIldCards,$matchSoundsLike,<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$matchAllWordForms,$forward,$wrap,$format,$replaceWith,<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$replace)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The first thing that must be done when working with Microsoft Word&rsquo;s automation model is to create an instance of the <b>word.application<\/b> COM object. This is done by using the <b>New-Object<\/b> cmdlet with the <b>&ndash;comobject<\/b> parameter. The returned <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb244569.aspx\"><font face=\"Segoe\">application object<\/font><\/a> is stored in the <b>$application<\/b> variable as shown here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$application = New-Object -comobject word.application<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the <b>application<\/b> object is created, the <b>visible<\/b> property is set to <b>$true<\/b>. This will make the Microsoft Word application visible and allow you to see the text as it is italicized. If you have multiple documents you wish to process, you will probably want to set the <b>visible<\/b> property to <b>$false<\/b> to speed things along. Setting the <b>visible<\/b> property to <b>$true<\/b> is seen here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$application.visible = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When you have the <b>application<\/b> object, you can use the <b>open<\/b> method from the <b>documents<\/b> collection to open the Microsoft Word document you wish to process. Store the returned <b>document<\/b> object in the <b>$document<\/b> variable, as shown here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$document = $application.documents.open(&#8220;C:fsoTest.docx&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You can create a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb259574.aspx\"><font face=\"Segoe\">selection object<\/font><\/a> directly from the <b>application<\/b> object by using the <b>selection<\/b> property. Store the returned <b>Selection<\/b> object in a variable called <b>$selection<\/b> to make your script easy to read:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$selection = $application.Selection<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When you have created your <b>Selection<\/b> object, it is time to create the array of words you want to italicize. If you only have a few words, you may want to hard code them directly into your script, as was done here. If you have a large number of words, you may want to store them in a text file and use the <b>Get-Content<\/b> cmdlet to create your array. In the ItalicWordsInWord.ps1 script, the words &ldquo;exchange&rdquo; and &ldquo;sql&rdquo; are stored in the array <b>$words<\/b>, as seen here.<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$words = &#8220;exchange&#8221;,&#8221;sql&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is now necessary to specify the parameters that will be used for the find-and-replace operation. The parameters of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb179352.aspx\"><font face=\"Segoe\">execute method<\/font><\/a> from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb258967.aspx\"><font face=\"Segoe\">find object<\/font><\/a> correspond to the selections available from the find and replace dialog box in Word. The variable names and the values are documented on MSDN but are relatively self-explanatory:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchCase = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchWholeWord = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchWildCards = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchSoundsLike = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$matchAllWordForms = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$forward = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$wrap = 1<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$format = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$replace = 2<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have assigned all the parameter values to the appropriate variables, use the <b>Foreach<\/b> statement to walk through the array of words to be italicized, which is stored in the <b>$words<\/b> variable. You will replace the word you find with the same word, as seen here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">Foreach ($word in $words)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$findText = $word<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$replaceWith = $word<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Use the italic property of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb258990.aspx\"><font face=\"Segoe\">font object<\/font><\/a> to italicize the replacement word. You can obtain the font object by using a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb259540.aspx\"><font face=\"Segoe\">replacement object<\/font><\/a> from the <b>find<\/b> object that was obtained from the <b>selection<\/b> object. This is seen here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$selection.find.replacement.font.italic = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have set the <b>italic<\/b> property to <b>$true<\/b>, you can call the <b>execute<\/b> method from the <b>Find<\/b> object. This is seen here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$exeRTN = $selection.find.execute($findText,$matchCase,<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$matchWholeWord,$matchWIldCards,$matchSoundsLike,<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$matchAllWordForms,$forward,$wrap,$format,$replaceWith,<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$replace)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the ItalicWordsInWord.ps1 script runs, the following appears:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of result of runnig the script\" alt=\"Image of result of runnig the script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1228\/hsg-12-28-09-01.jpg\" width=\"600\" height=\"420\"><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">FM, that is all there is to using Microsoft Word to italicize words in a Word document. Word Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/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 class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I do not know about you, but I hate having to italicize certain words. Actually, I hate italicizing all words because I do not like removing my hands from the keyboard, grabbing the mouse, and then searching for where the text formatting menus have moved in Office 2007. If I [&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-51703","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; &nbsp; Hey, Scripting Guy! I do not know about you, but I hate having to italicize certain words. Actually, I hate italicizing all words because I do not like removing my hands from the keyboard, grabbing the mouse, and then searching for where the text formatting menus have moved in Office 2007. If I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51703","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=51703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51703\/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=51703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}