Office Automation using C#

> Edraw Tip > Office Automation using C#
Posted by James Freeman |
Display MS Word document programmatically using C# on windows or web form using Edraw Office Viewer Active-X control.

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

free download5 MB

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

  1. Install Edraw Office Viewer Component.
  2. Create a blank c# application.
  3. Add Edraw Office Viewer Component on your form through customize toolbox window as shown below.

add office viewer component

  1. Drag and drop Office Viewer from toolbox on your form and resize as per requirement.

  2. 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.

Add Office Reference

  1. You can see all added reference on solution explorer as shown in figure.

  2. Two reference has been added for EDOffice and three libraries for Office & Word.

office references

  1. Define a variable for word document in public section of form as
    using Word = Microsoft.Office.Interop.Word;
    using System.Reflection;

  2. 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.

  3. 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;
    //do more

  4. 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.

open office document

  1. Click the Office Automation button, the automation code will be execute. Switch to print preview view.

execute office automation

Free Download Office Viewer Component and View Sample Projects

Free Download Office Viewer Component

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.

Embedding MS Word in VB.NET and Automating Word

Automating MS Word in Visual Basic

Automating Word with VBA and VBScript

Embed MS Excel in VB 6 and Do the Excel Automation

Embedding MS Word, Excel, PowerPoint into a WPF Application

download EdrawMind
main page

Get Started! You Will Love This Easy-To-Use Diagram Software

EdrawMax is an advanced all-in-one diagramming tool for creating professional flowcharts, org charts, mind maps, network diagrams, UML diagrams, floor plans, electrical diagrams, science illustrations, and more. Just try it, you will love it!