How to interoperate with COM components in Silverlight 4

Article translations Article translations
Article ID: 2451458 - View products that this article applies to.
Expand all | Collapse all

On This Page

INTRODUCTION

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

Collapse this imageExpand this image
codefx Medium

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:
Trusted applications

How to manipulate the Microsoft Word automation in Silverlight 4

  1. Run the following code to create a Microsoft Word automation object by using the AutomationFactory class:
    dynamic word = AutomationFactory.CreateObject("Word.Application");
    
  2. 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;
    
  3. Run the following code to print the document:
    doc.PrintOut();
    


How to manipulate Notepad in Silverlight 4

  1. Run the following code to create a WSHShell object by using the AutomationFactory class:
    dynamic shell = AutomationFactory.CreateObject("WScript.Shell");
  2. Run the following code to start Notepad:
    shell.Run(@"%windir%\notepad", 5);
  3. 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:

Collapse this tableExpand this table
LanguageProject Name
Visual C#CSSL4COMInterop
Visual Basic.NETVBSL4COMInterop

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

MORE INFORMATION

What is All-In-One Code Framework?

All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Microsoft website:

http://1code.codeplex.com

How to find more All-In-One Code Framework samples

To find more All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the following Microsoft Web site:
All-In-One Code Framework samples

REFERENCES

For more information about the AutomationFactory class, visit the following Microsoft website:
General information about the AutomationFactory class
For more information about how to use automation in trusted applications, visit the following Microsoft website:
How to use automation in trusted applications
For more information about Silverlight 4 tools for Visual Studio 2010, visit the following Microsoft website:
Silverlight 4 tools for Visual Studio 2010
For more information about Silverlight 4 runtime, visit the following Microsoft website:
Silverlight 4 runtime
For more information about Microsoft Office 2010, visit the following Microsoft website:
Microsoft 2010

Rapid publishing disclaimer
Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.

Properties

Article ID: 2451458 - Last Review: November 17, 2010 - Revision: 2.0
APPLIES TO
  • Microsoft Visual Studio 2010 Professional
  • Microsoft Visual Studio 2010 Ultimate
  • Microsoft Office 2007 Compatibility Pack Service Pack 1
  • Microsoft Office 2007 Service Pack 2 for Language Interface Pack
Keywords: 
kbcodefx kbinfo kbnomt kbsurveynew kbrapidpub atdownload KB2451458

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com