This article describes an All-In-One Code Framework sample that is available for download.
The code sample demonstrates how to interoperate with COM components and how to export data to Notepad or Microsoft Excel.
Difficulty level
Preconditions for interoperating with COM components in Silverlight 4
Interoperating with COM components is a new feature in Silverlight 4.To interoperate with COM, you must be running Silverlight 4 SDK, and you must enable the Require elevated trust when running outside the browser checkbox inthe Out-of-Browser Settings dialog box. To determine whether this feature is available, run the following code:
For more information about trusted applications, visit the following Microsoft website:How to manipulate the Microsoft Word automation in Silverlight 4
The code sample demonstrates how to interoperate with COM components and how to export data to Notepad or Microsoft Excel.
Difficulty level

Download information
To download this code sample, click the following link:Technical overview
Preconditions for interoperating with COM components in Silverlight 4
Interoperating with COM components is a new feature in Silverlight 4.To interoperate with COM, you must be running Silverlight 4 SDK, and you must enable the Require elevated trust when running outside the browser checkbox inthe Out-of-Browser Settings dialog box. To determine whether this feature is available, run the following code:
System.Runtime.InteropServices.Automation.AutomationFactory.IsAvailable
For more information about trusted applications, visit the following Microsoft website:
How to manipulate the Microsoft Word automation in Silverlight 4
- Run the following code to create a Microsoft Word automation object by using the AutomationFactory class:
dynamic word = AutomationFactory.CreateObject("Word.Application");
- Run the following code to create a new Word document, and to write text and apply formatting:
dynamic doc = word.Documents.Add();
dynamic range1 = doc.Paragraphs[1].Range;
range1.Text = "Silverlight4 Word Automation Sample\n";
range1.Font.Size = 24;
range1.Font.Bold = true;
- Run the following code to print the document:
doc.PrintOut();
How to manipulate Notepad in Silverlight 4
- Run the following code to create a WSHShell object by using the AutomationFactory class:
dynamic shell = AutomationFactory.CreateObject("WScript.Shell");
- Run the following code to start Notepad:
shell.Run(@"%windir%\notepad", 5);
- Run the following code to send keystrokes to Notepad by using the SendKeys method:
shell.SendKeys("Name{Tab}Age{Tab}Gender{Enter}");
Technology category
- Silverlight
- Office
Languages
This code sample contains the following programming languages:Language | Project Name |
Visual C# | CSSL4COMInterop |
Visual Basic.NET | VBSL4COMInterop |
Prerequisites
To run this code sample, you must be running the following products:- Silverlight 4 Tools for Visual Studio 2010
- Silverlight 4 runtime
- Microsoft Office 2007 or a later version of Microsoft Office
Tags
- Silverlight
- Office
- COM Interoperate