Archive

Archive for April, 2011

COM Automation in Silverlight

April 4, 2011 2 comments

To instantiate the COM object, we’re required to call it by it’s name, and the syntax is very similar to what we were doing years ago with classic ASP or VB script. Except, this time it’s the AutomationFactory class that provides the CreateObject method:

dynamic wordApplication = AutomationFactory.CreateObject(“Word.Application”);

[application].Documents.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format,                          Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)

The dynamic keyword is new in C# 4 syntax and provides the late-binding mechanism we knew back in the VB6/COM days. You can also see the use of dynamic keyword as a shorter syntax for reflection, all serving the purpose of dynamic objects not being type checked during the compilation, but rather at run time only.

Using the dynamic keyword also means you don’t get any intellisense help in Visual Studio which subsequently means you’ll end up reading a lot of help files to understand the API on objects you’re working with. For example, try this Word method for opening a single document:
Identifying Automation Servers
You identify Automation servers using programmatic identifiers or progIDs. When a user installs an application that provides Automation support, the application registers progID values so that external applications can locate them.

Silverlight can use only Automation servers that are already installed; therefore, the CreateObject and GetObject methods throw exceptions when they cannot find the requested progID.

The following list shows a few common progID values:

Office Automation: Outlook.Application, Excel.Application, PowerPoint.Application,Word.Application

System Automation: Scripting.FileSystemObject, WScript.Shell, Shell.Application

Windows Management Instrumentation: WbemScripting.SWbemLocator

In general, these strings map to particular versions of Automation servers. If multiple versions of a server are available, the server typically adds a version number to the progID.

To find documentation and information about these and other Automation servers, perform Web searches using phrases such as “PowerPoint object model” and “CreateObject PowerPoint.Application”. Note that most Automation documentation samples are in VBScript, which you need to adapt to your preferred Silverlight language.

Categories: SilverLight