Working with Document Objects
Word Developer Reference |
In Microsoft Visual Basic, the methods for modifying files are methods of the Document object or the Documents collection. This topic includes Visual Basic examples related to the following tasks:
- Creating a new document
- Opening a document
- Saving an existing document
- Saving a new document
- Activating a document
- Determining if a document is open
- Referring to the active document
Creating a new document
The Documents collection includes all of the open documents. To create a new document, use the Add method to add a Document object to the Documents collection. The following instruction creates a document.
|
A better way to create a document is to assign the return value to an object variable. The Add method returns a Document object that refers to the new document. In the following example, the Document object returned by the Add method is assigned to an object variable. Then, several properties and methods of the Document object are set. You can easily control the new document using an object variable.
|
Opening a document
To open an existing document, use the Open method with the Documents collection. The following instruction opens a document named Sample.doc located in the MyFolder folder.
|
Saving an existing document
To save a single document, use the Save method with the Document object. The following instruction saves the document named Sales.doc.
|
You can save all open documents by applying the Save method to the Documents collection. The following instruction saves all open documents.
|
Saving a new document
To save a single document, use the SaveAs method with a Document object. The following instruction saves the active document as "Temp.doc" in the current folder.
|
The FileName argument can include only the file name or the complete path (for example, "C:\Documents\Temporary File.doc").
Closing documents
To close a single document, use the Close method with a Document object. The following instruction closes and saves the document named Sales.doc.
|
You can close all open documents by applying the Close method of the Documents collection. The following instruction closes all documents without saving changes.
|
The following example prompts the user to save each document before the document is closed.
|
Activating a document
To change the active document, use the Activate method with a Document object. The following instruction activates the open document named Sales.doc.
|
Determining if a document is open
To determine if a document is open, you can enumerate the Documents collection by using a For Each...Next statement. The following example activates the document named Sample.doc if the document is open, or opens Sample.doc if it is not currently open.
|
Referring to the active document
Instead of referring to a document by name or by index number—for example,
|
—the ActiveDocument property returns a Document object that refers to the active document (the document with the focus). The following example displays the name of the active document, or if there are no documents open, it displays a message.
|