Office Automation using C#
Office Automation Introduction
This article explains how to display MS Word document
programmatically using C# on windows or web form using Edraw
Office Viewer Active-X control.
The examples demonstrate how to automate the following activities:
• Referencing Office Viewer ActiveX control on form
• Starting Word
• Do the Office Automation
Click Here to Download Edraw Office Component - Support Word, Excel, PowerPoint, Visio and Project
Office Automation Background
Office Automation is a process that allows applications to control other application for e.g. application developed using Visual Basic .NET or C# can programmatically control other applications. The automation process can be performed through the Object Model of the application which is controlled. Word exposes this programmatic functionality through an object model library. The object model is a collection of classes and methods that serve as counterparts to the logical components of Word. Automation to Word allows you to perform actions such as creating & opening documents, adding text to documents, saving documents in different formats, handling e-mail, mail merge, and formatting documents and much more. With Word and other Microsoft Office applications, nearly all of the functionalities which you would perform manually through the user interface are available to control through programmatically by using automation. You would find various objects such as Application, Document, Paragraph, Range etc., each of which contain the functionality of that component in Word.
You could have the Office Viewer Active-X control on your form to host word document, excel workbook and PowerPoint slideshow. Office Viewer Active-X control acts as an ActiveX document container for hosting Office documents in a custom form or Web page. The control is lightweight and flexible, and gives developers new possibilities for using Office in a custom solution.
Office Automation Steps
- Install Edraw Office Viewer Component.
- Create a blank c# application.
- Add Edraw Office Viewer Component on your form through customize toolbox window as shown below.
Drag and drop Office Viewer from toolbox on your form and resize as per requirement.
Now add reference for Word Object Library 11.0 from add reference on solution explorer. Here I am using Word 2003, so object library version is 11.0.
You can see all added reference on solution explorer as shown in figure.
Two reference has been added for EDOffice and three libraries for Office & Word.
Define a variable for word document in public section of form as
using Word = Microsoft.Office.Interop.Word;
Add two buttons to form and "Open", "Office Automation" as their text, on click event it would open a new word document then do a simple office automation.
Add following code for button click event.
private void btnOpen_Click(object sender, EventArgs e)
private void btnAuto_Click(object sender, EventArgs e)
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord = (Word._Application)axEDWord1.GetApplication();
//Start Word and create a new document.
Word._Document oDoc = (Word._Document)axEDWord1.ActiveDocument();
oDoc.ActiveWindow.DisplayRulers = false;
oDoc.ActiveWindow.DisplayScreenTips = false;
oDoc.ActiveWindow.DisplayHorizontalScrollBar = false;
oDoc.ActiveWindow.DisplayVerticalRuler = false;
oDoc.ActiveWindow.DisplayVerticalScrollBar = true;
oWord.ActiveWindow.View.Type = Word.WdViewType.wdPrintPreview;
Run the C# project then click the Open button. In the pop up dialog, you can choose the file path. Click ok. The word document will be opened in the form.
Click the Office Automation button, the automation code will be execute. Switch to print preview view.
Now you can add text, format it, send mail do all functionalities.
You can control all word functionalities to be made available or unavailable programmatically through code with all available objects, properties & Methods of word.
You can play around with all objects, properties and methods of word to control it programmatically through your code.