Software Architecture-Silverlight 4.0-New Feature-COM interop API changes

COM interop API changes

In Silverlight 4, System.Windows.Interop name space is renamed as System.Runtime.InteropServices.Automation and main static class used for COM Factory is renamed from ComAutomationFactory to AutomationFactory.

Lets consider a small example to automate MS Word via Silverlight Project.

Add namespace as System.Runtime.InteropServices.Automation to Silverlight page. Add refeerence of Microsoft.CSharp for type dynamic.

COM interop is supported only in Out-of-Browser applications with ElevatedPermission.
Incase of normall silverlight application exception is thrown with details as” This operation is not supported in the current context.”

Consider some manipulation of word document as below.

            if (App.Current.IsRunningOutOfBrowser && App.Current.HasElevatedPermissions)
                if (AutomationFactory.IsAvailable)
                    dynamic wordFactory = AutomationFactory.CreateObject("Word.Application");
                    dynamic doc = wordFactory.Documents.Add();
                    wordFactory.Visible = true;
                    doc.Content.Font.Name = "Tahoma";
                    doc.Content.Font.Size = 24;
                    doc.Content.Font.Bold = true;
                    doc.Content.Font.Italic = true;
                    doc.Content = "Hello Shahzad Sarwar- Testing......Testing......Testing......";
                    dynamic tblNew = doc.Tables.Add(doc.Range(0, 0), 6, 8);
                    int intCount = 1;
                    foreach (var cell in tblNew.Range.Cells)
                        cell.Range.InsertAfter("Cell " + intCount);


For details about dynamic usage see here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: