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