Silverlight 4: Creating Word Documents in MyDocuments Folder

Silverlight 4.0 supports COM automation. Using this feature we can interact with MS-Word, Excel etc via our application. The business requirement behind this isinteraction that a lot  users prefer to store their data in Word or Excel format for immediate accessibility and modification. A common scenario is that if they are using an application which is reading the data from the remote server using the WCF/Web service and now this data is to be stored on the local machine for local operations, then Word/Excel are the most desired products for these users. In Silverlight 4, using Out-Of-Browser feature with elevated rights and using AutomationFactory, we can easily build application integrating with Word/Excel/Outlook. In the article, I have demonstrated the same.

Step 1: Open VS2010 and create a Silverlight application, name it as ‘SL4_Accessing_MyDocuments’. In this project, add the reference to Microsoft.CSharp. Also right click on the project and go to properties and select the CheckBox for ‘Enable the application running out of browser’ as shown below:

Out Of Browser

Step 2: Click on the ‘Out-of-Browser Settings’ and select the CheckBox for ‘Required elevated trust when running outside the browser’ as shown below:


Step 3: Open MainPage.Xaml and write the following Xaml:

Silverlight COM Automation

Step 4: Open MainPage.Xaml.cs (the code behind) and use the following namespaces:
using System.Runtime.InteropServices.Automation;

Step 5: Write the following code on the click event of the ‘Create Word Document’ button:

private void btnCreteWordDocument_Click(object sender, RoutedEventArgs e)
         #region Reading from Word File
         //S1: Create a Word Application object with dynamic. 
         // This is late binding. 
         //The References will be resolved at RunTime.
         dynamic objWord = AutomationFactory.CreateObject        ("Word.Application");
         //S2: Add the new Word Document 
         dynamic document = objWord.Documents.Add();
         object startIndex = 0;
         //The Location for writing the Content in the Docuement always 0
          dynamic range = document.Range(ref startIndex);
          //S3: Define Font Name and the Content to be written 
         // (Here you can have your code retrived from the Service Call)
          range.Font.Name = "Times New Roman";
          range.InsertAfter("Hello World!This is the Simple Word File
                 Created using Silverlight 4.0 ");
          Random rnd = new Random();
          //S4: Access the MyDocuments Special Folder and Save the file
          string fileName = Environment.GetFolderPath      (Environment.SpecialFolder.MyDocuments) +
                "\\" + "MyFileSL" + rnd.Next().ToString() + ".docx";
          document.SaveAs(ref fileName);
          objWord.Visible = true;
     catch (Exception ex)

Please see the code carefully. This code creates an instance of the Word Application using AutomationFactory. The word document is created and the data is written in the document. Please read the comments carefully on every line to understand the code.

Step 6: Run the Application and Click on the Button and the word document will be visible with the data in it.

Conclusion: With this feature in Silverlight, our OOB application can easily interact with Special folders and helps to create documents. So all users using Document format for storing the data can make us of this.

Will you give this article a +1 ? Thanks in advance

About The Author

Mahesh is having 10 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). He also blogs regularly at Follow him on twitter @maheshdotnet

No comments: