December 28th, 2009

Hey, Scripting Guy! How Can I Italicize Specific Words in a Microsoft Word Document?

Bookmark and Share 

 

Hey, Scripting Guy! Question

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 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!

— FM

 

Hey, Scripting Guy! AnswerHello FM,

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—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’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 Earl Grey tea in the afternoon, but it is entirely your choice).

FM, it’s also your choice to italicize your company’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.

ItalicWordsInWord.ps1

$application = New-Object -comobject word.application

$application.visible = $true

$document = $application.documents.open(“C:fsoTest.docx”)

$selection = $application.Selection

$words = “exchange”,”sql”

$matchCase = $false

$matchWholeWord = $true

$matchWildCards = $false

$matchSoundsLike = $false

$matchAllWordForms = $false

$forward = $true

$wrap = 1

$format = $true

$replace = 2

Foreach ($word in $words)

{

 $findText = $word

 $replaceWith = $word

 $selection.find.replacement.font.italic = $true

 $exeRTN = $selection.find.execute($findText,$matchCase,

 $matchWholeWord,$matchWIldCards,$matchSoundsLike,

 $matchAllWordForms,$forward,$wrap,$format,$replaceWith,

 $replace)

 }

The first thing that must be done when working with Microsoft Word’s automation model is to create an instance of the word.application COM object. This is done by using the New-Object cmdlet with the –comobject parameter. The returned application object is stored in the $application variable as shown here:

$application = New-Object -comobject word.application

After the application object is created, the visible property is set to $true. 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 visible property to $false to speed things along. Setting the visible property to $true is seen here:

$application.visible = $true

When you have the application object, you can use the open method from the documents collection to open the Microsoft Word document you wish to process. Store the returned document object in the $document variable, as shown here:

$document = $application.documents.open(“C:fsoTest.docx”)

You can create a selection object directly from the application object by using the selection property. Store the returned Selection object in a variable called $selection to make your script easy to read:

$selection = $application.Selection

When you have created your Selection 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 Get-Content cmdlet to create your array. In the ItalicWordsInWord.ps1 script, the words “exchange” and “sql” are stored in the array $words, as seen here.

$words = “exchange”,”sql”

It is now necessary to specify the parameters that will be used for the find-and-replace operation. The parameters of the execute method from the find object 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:

$matchCase = $false

$matchWholeWord = $true

$matchWildCards = $false

$matchSoundsLike = $false

$matchAllWordForms = $false

$forward = $true

$wrap = 1

$format = $true

$replace = 2

After you have assigned all the parameter values to the appropriate variables, use the Foreach statement to walk through the array of words to be italicized, which is stored in the $words variable. You will replace the word you find with the same word, as seen here:

Foreach ($word in $words)

{

 $findText = $word

 $replaceWith = $word

Use the italic property of the font object to italicize the replacement word. You can obtain the font object by using a replacement object from the find object that was obtained from the selection object. This is seen here:

 $selection.find.replacement.font.italic = $true

After you have set the italic property to $true, you can call the execute method from the Find object. This is seen here:

 $exeRTN = $selection.find.execute($findText,$matchCase,

 $matchWholeWord,$matchWIldCards,$matchSoundsLike,

 $matchAllWordForms,$forward,$wrap,$format,$replaceWith,

 $replace)

 }

When the ItalicWordsInWord.ps1 script runs, the following appears:

Image of result of runnig the script

 

FM, that is all there is to using Microsoft Word to italicize words in a Word document. Word 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.