How to: 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 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.

Saving a Document Without Changing the Name

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.

    Me.Application.ActiveDocument.Save()
    
    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.

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

Saving a Document With a New Name

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

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

    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.

    Me.SaveAs("C:\Test\NewDocument.doc")
    
    object fileName = @"C:\Test\NewDocument.doc"; 
    
    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

    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.

    Me.Application.ActiveDocument.SaveAs("C:\Test\NewDocument.doc")
    
    object fileName = @"C:\Test\NewDocument.doc";
    
    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);
    

Compiling the Code

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.

See Also

Tasks

How to: Close Documents

How to: Open Existing Documents

Concepts

Document Host Item

Optional Parameters in Office Solutions