Hey, Scripting Guy! Question

Hey Scripting Guy! We are currently beta testing Microsoft Office 2010. It is awesome by the way, and I need to figure out what version of Word we have installed on our computers. I know there are different versions, and I would like to get some kind of inventory before we do the upgrade. How can I determine the version of Word that is installed on our computers?

-- CC

Hey, Scripting Guy! Answer

Hello CC,

Microsoft Scripting Guy Ed Wilson here. One of the cool things about Facebook is the way we can keep up to date with our friends, co-workers, and fans of the Hey Scripting Guy! articles. This morning, for example, I found out that my buddy Pete in Australia just finished his certification as a cave diver. For those of you who might not know, cave diving is awesome, but can also be potentially dangerous if done incorrectly. This is why it is important to have advanced training before attempting to go spelunking underwater. The potential for silting (fine dirt on the bottom that gets mixed up with the water turning it into soup) to reduce visibility to zero can make finding your way out of the cave nearly impossible unless you have a line to follow.

CC, trying to write scripts is certainly not as dangerous as cave diving, but it can certainly be as confusing as a silted cave. Luckily, you have a line that you can follow—our new Script Center Script Gallery. I did a search on our Script Gallery and found a really cool script that displays the version of Word. It is a VBScript; however, we can use it as a line to follow to lead us to a nice Windows PowerShell script. The DetermineTheVersionOfMicrosoftWordInstalledOnAComputer.vbs VBScript is seen here.  

DetermineTheVersionOfMicrosoftWordInstalledOnAComputer.vbs

Set objWord = CreateObject("Word.Application")

Wscript.Echo "Version: " & objWord.Version

Wscript.Echo "Build: " & objWord.Build

Wscript.Echo "Product Code: " & _

    objWord.ProductCode()

objWord.Quit

The first thing the DetermineTheVersionOfMicrosoftWordInstalledOnAComputer.vbs script does is create an instance of the Word.Application object.  In VBScript you used the CreateObject command to create a COM object. In Windows PowerShell, you use the New-Object cmdlet to create a new object. If the object you want to create is a COM object, you use the comobject parameter. You do not need to use quotation marks when specifying the program ID of the COM object. This is seen here:

$objword = new-object -comobject word.application

VBScript required you to use Wscript.Echo or some other command to produce output from the script. Because Windows PowerShell was designed as a console application, it does not require anything special to display information in the Windows PowerShell console. If you put something in quotation marks, it will automatically be displayed in the Windows PowerShell console. To concatenate (glue stuff together), you use the plus sign (+). The version of Microsoft Word is a property of the Word.Application object. MSDN documents many methods and properties from the Word.Application object. To display the version, you print a string that states it is the version and use the concatenation operator to display the Word version. This is seen here:

"version: " + $objword.version

You use exactly the same process to display the build number of Word:

"build: " + $objword.build

To get the product code of Microsoft Word, you have to use a method. This is documented in MSDN's description of the ProductCode method. When calling a method in Windows PowerShell, you must place parentheses at the end of the method name. This is seen here:

"productcode: " + $objword.productcode()

The last thing you must do is call the quit method from the Word.Application object. If you do not, you will end up with several instances of WINWORD.EXE running on your computer, as seen here:

Image of more than one instance of WINWORD.EXE running at the same time


The quit method is defined on the Application object and will accept three optional parameters. You do not need to save any changes, specify the formatting, or route the document to the next recipient. You can call the method with no parameters, and it will simply exit the application and release the object from memory. This is seen here:

$objword.quit()

The completed DetermineTheVersionOfMicrosoftWordInstalledOnAComputer.ps1 script is seen here.

DetermineTheVersionOfMicrosoftWordInstalledOnAComputer.ps1

$objword = new-object -comobject word.application

"version: " + $objword.version

"build: " + $objword.build

"productcode: " + $objword.productcode()

$objword.quit()

When the script runs, the following output is displayed.

Image of the output of the script


It might be a bit more useful to check if a specific version of Word is installed on a computer. To do this, you can easily turn the script into a function. To turn the script into a function, you use the function keyword, and give the function a name, such as Get-WordVersion. Inside the code block (the curly brackets), you copy the code from the previous script. You do not want to return anything but the Word version number, so the function only retrieves the Word version property. The function will automatically return the value to the calling code. This is seen here:

Function Get-WordVersion

{

 $objword = new-object -comobject word.application

 $objword.version

 $objword.quit()

} #end Get-WordVersion

To call the function and to evaluate the Word version, you can use the if statement. This is seen here:

If(Get-WordVersion -eq 12) { "Word 12 found" } else { "Word 12 not found" }

The complete DetermineTheVersionOfMicrosoftWordInstalledOnAComputerFunction.ps1 script is seen here.

DetermineTheVersionOfMicrosoftWordInstalledOnAComputerFunction.ps1

Function Get-WordVersion

{

 $objword = new-object -comobject word.application

 $objword.version

 $objword.quit()

} #end Get-WordVersion

 

# *** Entry Point to Script ***

 

If(Get-WordVersion -eq 12) { "Word 12 found" } else { "Word 12 not found" }

When the DetermineTheVersionOfMicrosoftWordInstalledOnAComputerFunction.ps1 script runs, the following output is displayed.

Image of the output of the script

CC, that is all there is to determining the version of Microsoft Word that is installed on your computer. This also ends our week of perusing the new Script Gallery.

Join us tomorrow as we dive into the virtual mail bag and come up with an assortment of questions that do not require long answers. That’s right! It’s the return of Quick-Hits Friday. If you want to keep up to date on activities on the Script Center follow us on Twitter or on Facebook. If you have questions, you can post them on the Official Scripting Guys Forum, or send them by e-mail to scripter@microsoft.com. Until tomorrow, have a great day!


Ed Wilson and Craig Liebendorfer, Scripting Guys