Friday, November 21, 2008

Try/Catch with the Open Office XML Tool Kit in PowerShell

In a script I posted to my PowerShell Scripts blog, I used both the Open XML SDK and the Try/Catch functions of PowerShell V2. The Office Open XML SDK is available from Microsoft as a free download. However, like some parts of the MSDN Library, Community Content has not been activated (Open Office XML SDK is not OPEN could have been the headline of this post!), at least yet.

The XML API is built on top of the System.IO.Packaging and provides classes to manipulate Open XML Documents. For developers that are using the Open XML APIs to create Office documents, the ability to access and manipulate the documents via PowerShell can provide two benefits. First, the developer can use PowerShell as a prototyping tool, to ensure the logic is right before committing the code to C#. Second, the developer can develop simple sanity checking scripts to do things like validate a document, etc.

The other interesting thing about this script is the Try/Catch logic. With V2, we finally have the try/catch/finally error handling features that C# programmers love. I’ll be posting more about this in future articles.

No comments: