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

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

Posted on 11/4/2008 2:13:49 AM

Hi Vikram,

I have the same problem as Surender. The application works fine in VS2005 but not in IIS. I have also checked all the access and permissions. there is no other winword.exe on my taskmgr except the one that i called in my program.
The program hangs after that.

Any help is greatly appreciated.

Thanks again.

Posted on 1/15/2009 8:20:54 AM

Surender, bn,
To make it visible for User try to add follows:
oWord.Application.Visible = true;

Posted on 1/24/2009 10:08:57 PM

I know this is an oldie, but still I was looking around. Office Automation, be it with 2007 or the previous versions always has been a pain to actually port in a working IIS environment. I happened to actually do it, with the use of "impersonate" in the web.config file.

You might also consider the fact that every file that you process will start an instance of word. And saddly, even though you close it in the code (for 2003 at the very least) it doesn't always close the instance of word.exe...

If I were to do it again, I'd use one of those API sold on the net that supposedly allow you to play with word without installing office.

Or simply do like me, switch to PDF. It's much more easier to manage on the long run for the code. And you can convert most of your word documents to PDF.

Cya !

Alex

Posted on 2/9/2009 12:51:58 AM

Hi Guru's

I need to open a word document and reading the text content. My program will be running successfully when i ran it under administrator account. But when I run my application using the application pools account , it fails without throwing any error .

The particular code that fail to run is :
Dim newApp As New Microsoft.Office.Interop.Word.Application()

From the task manager, i can see that the WINWORD.exe is running under the application pools account that is assigned to the application. I have verified that the word application can be launch manually using the application pools account and in the Component Services, I have granted the permission for the application pools account to Local Launch , remote launch, local activation, remote activion

Any idea how to solve this issue ? what kind of permission ?

Kindly advice

Thanks

Posted on 3/10/2009 1:12:49 AM

I have the same problem as Surender. The application works fine in VS2005 but not in IIS. I have also checked all the access and permissions. there is no other winword.exe on my taskmgr except the one that i called in my program.
The program hangs after that.

Any help is greatly appreciated

Posted on 2/10/2010 2:34:52 PM

hi i want to read the contents of a word file, modify them and then paste them back into another word file...

I know the code to read a doc file... but how can we read all the tables and images present in that doc file.???

I have to manipulate only the text part and then paste the entire contents of file-1 (text, tables, images) into file-2.

Thanks

Posted on 4/2/2010 5:51:20 AM

Sir, I want to include the Images of the graphs generated from my appliction into the Word document.

Please can u help me with the code for it?

Front end for my appliction is Asp.net using C# .

Posted on 4/2/2010 5:52:14 AM

Sir, I want to include the Images of the graphs generated from my appliction into the Word document.

Please can u help me with the code for it?

Front end for my appliction is Asp.net using C# .

Posted on 5/8/2010 9:01:08 AM

use inlineshapes class to inser images in the word document.

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 - 2010 Vikram Lakhotia