Aracılığıyla paylaş


How to: Save Documents

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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.

Saving a Document Without Changing the Name

To save the document associated with a document-level customization

To save the active document

  • Call the _DocumentSave 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()
    
    object fileName = @"C:\Test\NewDocument.doc"; 
    this.Application.Documents.get_Item(ref fileName).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 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 Microsoft.Office.Tools.Word.Document class, 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 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.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

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

The Variable missing and Optional Parameters in Office Solutions