How to: Programmatically Save Documents

There are several ways to save Microsoft Office Word documents. You can save a document without changing the name of the document, or you can save a document with a new name.

Applies to: The information in this topic applies to document-level projects and application-level projects for Word 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

To save the document associated with a document-level customization

To save the active document

  • Call the _Document.Save method for the active document. To use this code example, run it from the ThisDocument or ThisAddIn class in your project.

    this.Application.ActiveDocument.Save();
    

If you are not sure whether the document you want to save is the active document, you can refer to it by its name.

To save a document specified by name

  • Use the document name as an argument to the Documents collection. To use this code example, run it from the ThisDocument or ThisAddIn class in your project.

    this.Application.Documents[@"C:\Test\NewDocument.docx"].Save();
    

Use the SaveAs method to save a document with a new name. You can use this method of the Microsoft.Office.Tools.Word.Document host item in a document-level Word project, or of a native Microsoft.Office.Interop.Word.Document object in any Word project. This method requires that you specify the new file name, but other arguments are optional.

Note Note

If you show the SaveAs dialog box inside of the DocumentBeforeSave event handler of ThisDocument and set the Cancel parameter to false, the application might quit unexpectedly. If you set the Cancel parameter to true, an error message appears indicating that Autosave has been disabled.

To save the document associated with a document-level customization with a new name

  • Call the SaveAs method of the ThisDocument class in your project, using a fully qualified path and file name. If a file by that name already exists in that folder, it is silently overwritten. To use this code example, run it from the ThisDocument class.

    Note Note

    The SaveAs method throws an exception if a target directory does not exist or if there are other problems saving a file. It is a good practice to use a try…catch block around the SaveAs method or inside a calling method.

    object fileName = @"C:\Test\NewDocument.docx"; 
    
    this.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);
    

To save a native document with a new name

  • Call the SaveAs method of the Microsoft.Office.Interop.Word.Document that you want to save, using a fully qualified path and file name. If a file by that name already exists in that folder, it is silently overwritten.

    The following code example saves the active document with a new name. To use this code example, run it from the ThisDocument or ThisAddIn class in your project.

    Note Note

    The SaveAs method throws an exception if a target directory does not exist or if there are other problems saving a file. It is a good practice to use a try…catch block around the SaveAs method or inside a calling method.

    object fileName = @"C:\Test\NewDocument.docx";
    
    this.Application.ActiveDocument.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);
    

This code example requires the following:

  • To save a document by name, a document named NewDocument.doc must exist in a directory named Test on drive C.

  • To save a document with a new name, a directory named Test must exist on drive C.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft