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

Published Wednesday, July 02, 2008 7:57 AM by vik20000in

Comments

# Programmatically replace word with the help of Word Automation

Saturday, July 05, 2008 2:16 AM by Community Blogs

Hi, In My last two posts I talked about · How to open and close a word document programmatically . ·

# re: Word Automation saving word document in the different Format

Tuesday, July 15, 2008 8:17 AM by John McDowall

Hi,

Do you know how to deal with a "Command Failed" error when executing SaveAs ?

# re: Word Automation saving word document in the different Format

Wednesday, July 16, 2008 12:06 AM by vik20000in

HI John,

I have never faced this problem as yet. When do u get this error?

# re: Word Automation saving word document in the different Format

Friday, October 17, 2008 4:54 PM by Jhony Moncada

Third criteria is just what I was looking for! Thank you so much!

# re: Word Automation saving word document in the different Format

Friday, January 08, 2010 10:23 AM by Jim

thanks for that very basic post which gives us no new information whatsoever! What other gems of information are you going to share with us??

Leave a Comment

(required) 
(required) 
(optional)
(required)