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 Microsoft Office version 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
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
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 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
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);
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.
Tasks
Concepts