Share via


Opening, Creating, Saving, and Closing New Documents

You use the Documents collection's Open method to open an existing document. The FileName argument can include the full path to the file or the file name alone. If the file specified in the FileName argument does not include the full path to the document, Word looks for the document in the current directory. If Word can't find the file by using the file path and file name specified in the FileName argument, an error occurs.

Instead of using a hard-coded path and file name, you can use the FileSearch object to make sure the file exists before trying to open it. You can also create a Dialog object that represents the File Open dialog box and use it to let the user select the file name to use as the FileName argument of the Open method.

You create a new document by using the Documents collection's Add method. The Add method can accept up to two optional arguments. You use the Template argument to specify the template on which to base the new document. If you leave this argument blank, the new document is based on the Normal.dot template. The NewTemplate argument is a Boolean value that specifies whether to create the new document as a template. The following example creates a new document based on the Normal.dot template:

Dim docNew As Word.Document

Set docNew = Documents.Add
With docNew
   ' Add code here to work with the new document.
End With

The method you use to save a document depends on whether the document is new or has already been saved. To save an existing document, you use the Document object's Save method. To save a new document, you use the Document object's SaveAs method and specify a file name in the method's FileName argument. If you use the Save method on a new document, Word displays the Save As dialog box to prompt the user to give the document a name.

You can also save a new document as soon as it is created by using the Add method and the SaveAs method together as follows:

Documents.Add.SaveAs FileName:="c:\my documents\fastsave.doc"
Set docNew = Documents("fastsave.doc")

If you use the Documents.Add.SaveAs syntax, you will not be able to set a Document object variable at the same time you use the Add method. Instead, you can refer to the newly created document by using the ActiveDocument property or by using the document's name in the Documents collection, as shown in the preceding example.

To close a document, you use the Document object's Close method. If there are changes to the document and you do not specify the SaveChanges argument, Word prompts the user to save changes. To prevent this prompt from appearing, use either the wdDoNotSaveChanges or the wdSaveChanges built-in constant in the Close method's SaveChanges argument. To close all open documents at once, use the Documents collection's Close method and either the wdDoNotSaveChanges or the wdSaveChanges constant in the SaveChanges argument.

See Also

Working with Microsoft Word Objects | The Document Object | Working with the Document Object