Share via


How to: Programmatically save documents

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.

Save a document without changing the name

To save the document associated with a document-level customization

  1. Call the Save method of the Document class. To use this code example, run it from the ThisDocument class in your project.

    Me.Save()
    
    this.Save();
    

To save the active document

  1. Call the 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

  1. 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.docx").Save()
    
    this.Application.Documents[@"C:\Test\NewDocument.docx"].Save();
    

Save 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 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

  1. 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 atry...catch block around the SaveAs method or inside a calling method.

    Me.SaveAs("C:\Test\NewDocument.docx")
    
    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

  1. Call the SaveAs method of the 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.docx")
    
    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);
    

Compile 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