Microsoft word 2007 automation 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


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 7/7/2008 11:25:20 AM

Hi,

Do you know of any other way to Automate MS word 2007 other than using PIA or COM. Does WPF or WCF allow automation from VS 2008?

Thank you.

Posted on 7/10/2008 8:29:21 PM

Hi Argenka,

I dont have any idea on working with word in WPF or WCF. But As far my knowledge its not possible.

Posted on 7/21/2008 9:10:39 AM

Hi Vikram,

I'm trying to develop some software to process word files with some word macro. do you know where I can find help with this or any reference book that i should know of ?
or can you help me ? ;o)

thanks in advance !
enz0

Posted on 7/24/2008 11:03:09 PM

Hi Vikram,

I have tried above method to open word doc with ASP.Net It is working fine on VS 2005 development server when I run from IIS it is not giving any response and not giving any error. I have give all permission to IIS aspnet user.

it is opening winword.exe on ASpNEt user on process but not showing out.

plz help me in this issue.
based on this I want to open Ms Access forms with asp.net

Posted on 7/25/2008 3:56:59 AM

Hi Surender,

The problem might be that another process is accessing the word document that you are trying to open. Before you try to open the document, see to it there is no winword.exe in the task manager. Also make sure that the word document you are trying to open is not opened by another user. You can also try and open the word document in readonbly mode and see if the document is opening or not.

Posted on 7/28/2008 1:06:59 PM

Hi Vikram
Can you help me to run this on VS2008, Asp.net 3.5 with Office 2007.
The VS2008 does not like the "Microsoft.Office.Interop.Word.Application()" and can not creat a word object.

Posted on 8/2/2008 4:07:05 AM

Hi Vikram,

Could you please answer if Word automation helps in getting a collapsible content in word document?

Thanks
Jyotsna

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia