Monday, 23. February 2009 19:26
At Login Consultants we have created a rather extensive scripting framework based on PowerShell scripts called Solution4. Conceptually it is similar to an earlier version of Solution4 which was based on CMD scripting and a collection of tools. The power of PowerShell, however, allowed us to improve this scripting framework even beyond our own dreams.
One of the key features of this scripting framework is that we standardize the automation and even more important, the way HOW automation is performed. This extensive standardization allows us to automatically document anything we do within this scripting framework. For our documentation efforts we chose Microsoft Word 2007 because it is, or will be, the most widely used word processor and because the COM interface is relatively straightforward.
The code snippets I will post in these series about PowerShelling Word 2007 are the functions I created to create items like paragraphs, tables and headings. At the end of these series I will put the complete script online for download.
First of all, lets get Word 2007 to run from PowerShell. Word 2007 exposes its methods through a COM interface. Starting an instance of Word 2007 in PowerShell is done like this:
$msWord = new-object -com Word.Application
$msWord.Visible = $true
This first line of code will start Word 2007 but it will be invisible to you. The second line will instruct Word 2007 to materialize. While this is already pretty cool, as you can see there’s no document. The next step is to open a new document like so:
$doc = $msword.documents.add()
The document created is based on the default template. If you want to base you new document on a different template, just tell Word. See:
$doc = $msword.documents.add(“D:\GenDoc\gendoc.dotx”)
Next time I will show you how to actually add some text.