Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Automating Microsoft Word 2007 with C Sharp

Hi,

Some time in our project we need to work with word (winword) document programmatically. Word provides a good way to automate nearly all the functionality that can be used through interface.  The process of programmatically working with word document is commonly termed as word Automation.

I will be using a series of blogs to discuss some of the functionality that can be achieved through word automation. In this Blog I will discuss how we can start programmatically open a new or and existing word document and close it.

To work with word 2007 automation we need to have the word 2007 installed in the machine where the application will run. When we install the word 2007, it also installs with it the com component required to automate word 2007.

To start working on the word automation we first need to reference the dll in the project. For this you need to click on the add reference in the project and go to the Com tab. There find the com component called Microsoft Office 12.0 Object Library. The namespace used for the word automation is Microsoft.Office.Interop.Word.

To create or open an existing word document we first need to open the word application. Then open or create the word document and last but not the least close the document.

Here is a code on how to do it. Remember to work with word programmatically; word takes a lot of parameter which might not be required in normal working. In most of the cases we will pass missing value to these parameters. Also all the value passed to word document needs to be passed as reference and not as value. Here is a code snippet of how to open a new word document.

//Create an object for missing values. This will be passed when ever we don’t want to pass value

Object missing = System.Reflection.Missing.Value();

//Objects for true and false to be used in the word document for passing true or false.

Object true = true;

Object false = false;

 

        

//Creating objects of word and document

Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

       

//Adding a new document to the application

oWordDoc = oWord.Documents.Add(ref missing, ref missing, ref missing, ref missing);

 

// To open an existing word document we need to pass the path of the existing document with some // other parameters. Here is the code snippet opening an existing word document

 

object fileName = “Path_of_the_file_to_Open”;

// You can keep it true if you want to open the file in readonly mode

object readOnly = false;

// we can keep it false if you want to open the file but not make it invisible

object isVisible = true;

oWordDoc = oWord.Documents.Open(ref fileName, ref missing, ref 

                  readOnly, ref missing, ref missing, ref missing, ref 

                  missing, ref missing, ref missing, ref missing, ref

                  missing, ref isVisible, ref missing, ref missing, ref

                  missing, ref missing);

 

To close the word document properly. We first need to close the word document and then the word application. If we do not close the word application then the memory will never be released for the word application.

//Closing the file

oWordDoc.Close(ref oFalse, ref missing, ref missing);

 

//Quitting the word application to release the memory.

oWord.Quit(ref missing, ref missing, ref missing);

Vikram

8 Comments

  • so easy... hopefully don't need to experience the pain in VB

  • Has anyone tried to access Word doc tables using this dll? I would like to retrieve values entered to a specific column in a Word doc table. I'd appreciate your thoughts and insights.

  • It's working perfect with me on win 2003(IIS6). After publishing it to our new Win2008(IIS7) It gave me the following error:

    System.UnauthorizedAccessException was unhandled by user code
    Message="Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005."
    Source="App_Web_o3gwip7c"

    This error occurs on the following line of code:

    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();

    Does anyone tried this on IIS7?

  • thanx a lot, but how can I open word doc embedded in my winApp

  • I FIXED IT!!! Just launch (comexp.msc) and fixed permissions... Component Services-> Computers-> My Computer -> DCOM Config-> Microsoft Word Document-> Properties-> Security

    1. Add NETWORK SERVICE to "Launch and Activation" with ALL checks enabled
    2. Add NETWORK SERVICE to "Access Permissions" with BOTH checks enabled


    Happy Coding,

    Waseem

  • tank you very much

  • Hi Waseem,
    I cannot find Microsoft Word Document in DCOM config?
    I have Word 2007 installed - do you have a different version?
    Do you have any other ideas why I cannot find it?
    Cheers

  • I have the same issues as trust20021 and J Staniforth. Help please. I've been over a week at this and I'm running out of resources.

Comments have been disabled for this content.