Document object

The Document object allows you to work with a document. The Documents collection contains all of the currently open documents in a Word application.

Creating a new document

Use the Add method to create a new document and add it to the Documents collection.

{Add a new document to the Documents collection.}
document = app.Documents.Add();

Opening an existing document

Use the Open method to open an existing document and add it to the Documents collection.

{Open an existing document.}
document = app.Documents.Open("C:\RESM\RESM.DOC");

Retrieving an open document

You can retrieve an open document from the Documents collection with the Item method by the number associated with the document.

{Retrieve the first document in the Documents collection.}
document = app.Documents.Item(1);

You can also retrieve documents by the name of the document.

{Retrieve the document named "RESM.DOC".}
document = app.Documents.Item("RESM.DOC");

Saving a document

To save a new document, use the SaveAs method.

{Save a new document.}
document.SaveAs("C:\RESM\RESM.DOC");

If you have previously saved a document, use the Save method to save the document.

{Save an existing document.}
document.Save();

You can use the Saved property to find out if a document has been saved. If the Saved property returns true, the document has been saved. Otherwise, it has not.

{If a document has not been saved, save it.}
if(not document.Saved) then
	document.Save();
end if;

You can also set the value of the Saved property. If you set the Saved property to true without saving a document, any messages prompting you to save the document are suppressed.


Closing a document

Use the Close method to close a document. You can use the constants contained in the wdSaveOptions enumeration to specify whether a document is saved when it is closed.

{Save changes and then close a document.}
document.Close(Word.wdSaveChanges);


Documentation Feedback