Word Automation saving word document in the different Format

Hi,

In my last post I talked about how to open and close a word document programmatically. In this post I will talk about how we can save the word document.

While using a word document there can be three kind of save functionality that can be required.

1.       When we open an existing word document and make some changes to the word document and save the word document.

2.       When we open a new document and want to save the document in a specified location

3.       When we want to save the word document in another format (Like XML, Compatible to word 97 etc..)

For the first criteria we can simply call the save method of the word document. This function does not take any parameter and saves the document at the same location where it was opened. Here is the code snippet.

oWordDoc.Save();

For the second option we need to use the SaveAs function of the word document object.  The function takes many parameter, but the most important and required parameter in the first parameter which takes the path where the document will be saved.

object fileName = “FullPath_for_saving_the_word_document”;

oWordDoc.SaveAs(ref fileName, ref missing, ref missing, ref 

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

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

                missing, ref missing, ref missing);

For the third option we need to pass one more parameter (second parameter) to the saveAs function telling the format in which the document should be saved.  The format has to be of type object but made form one of the values in the enumeration Microsoft.Office.Interop.Word.WdSaveFormat

object fileName = “FullPath_for_saving_the_word_document”;
object Format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
or
//object Format = Microsoft.Office.Interop.Word.WdSaveFormat. wdFormatXML;

oWordDoc.SaveAs(ref fileName, ref Format, ref missing, ref 

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

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

                missing, ref missing, ref missing);

Vikram

2 Comments

Comments have been disabled for this content.