Working with Document objects

In Visual Basic, the methods for modifying files are methods of the Document object or the Documents collection object.

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 new document.

Documents.Add

A better way to create a new 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, newDoc. Then several properties and methods of the Document object are set. You can easily control the new document using the newDoc object variable.

Set newDoc = Documents.Add
With newDoc
    .Content.Font.Name = "Arial"
    .SaveAs FileName:="Sample.doc"
End With

Opening a document

To open an existing document, use the Open method with the Documents collection. The following instruction opens a document named "MyDocument.doc" located in the folder named "MyFolder."

Documents.Open FileName:="C:\MyFolder\MyDocument.doc"

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.

Documents("Sales.doc").Save

You can save all open documents by applying the Save method to the Documents collection. The following instruction saves all open documents.

Documents.Save

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.

ActiveDocument.SaveAs FileName:="Temp.doc"

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.

Documents("Sales.doc").Close SaveChanges:=wdSaveChanges

You can close all open documents by applying the Close method to the Documents collection. The following instruction closes all documents without saving changes.

Documents.Close SaveChanges:=wdDoNotSaveChanges

The following example prompts the user to save each document before the document is closed.

For Each aDoc In Documents
    aDoc.Save NoPrompt:=False
    aDoc.Close
Next

Activating a document

To change the active document, use the Activate method with a Document object. The following instruction activates the open document named MyDocument.doc.

Documents("MyDocument.doc").Activate

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's not currently open.

For Each aDoc In Documents
    If InStr(1, aDoc.Name, "sample.doc", 1) Then 
        aDoc.Activate
    Else
        docFound = False
    End If
Next aDoc
If docFound = False Then _
    Documents.Open FileName:="C:\Documents\Sample.doc"

Referring to the active document

Instead of referring to a document by name or index number — for example Documents("Sales.doc") — the ActiveDocument property returns a Document object which 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.

If Documents.Count >= 1 Then 
    MsgBox ActiveDocument.Name
Else
    MsgBox "No documents are open"
End If