Documents Collection Object

Multiple objects
Multiple objects

A collection of all the Document objects that are currently open in Word.

Using the Documents Collection

Use the Documents property to return the Documents collection. The following example displays the names of the open documents.

For Each aDoc In Documents
    aName = aName & aDoc.Name & vbCr
Next aDoc
MsgBox aName

Use the Add method to create a new empty document and add it to the Documents collection. The following example creates a new document based on the Normal template.


Use the Open method to open a file. The following example opens the document named "Sales.doc."

Documents.Open FileName:="C:\My Documents\Sales.doc"

Use Documents(index), where index is the document name or index number to return a single Document object. The following instruction closes the document named "Report.doc" without saving changes.

Documents("Report.doc").Close SaveChanges:=wdDoNotSaveChanges

The index number represents the position of the document in the Documents collection. The following example activates the first document in the Documents collection.



The following example enumerates the Documents collection to determine whether the document named "Report.doc" is open. If this document is contained in the Documents collection, the document is activated; otherwise, it's opened.

For Each doc In Documents
    If doc.Name = "Report.doc" Then found = True
Next doc
If found <> True Then 
    Documents.Open FileName:="C:\Documents\Report.doc"
End If

Properties | Application Property | Count Property | Creator Property | Parent Property

Methods | Add Method | CanCheckOut Method | CheckOut Method | Close Method | Item Method | Open Method | Save Method

Parent Objects | Application | Global

Child Objects | Bookmarks | Characters | Comments | Email | Endnotes | Envelope | Fields | Footnotes | FormFields | Frames | Frameset | HTMLDivisions | Hyperlinks | Indexes | InlineShapes | ListParagraphs | Lists | ListTemplates | Mailer | MailMerge | PageSetup | Paragraphs | ProofreadingErrors | Range | ReadabilityStatistics | Revisions | RoutingSlip | Sections | Sentences | Shape | Shapes | SmartTags | StoryRanges | Styles | StyleSheets | Subdocuments | Tables | TablesOfAuthorities | TablesOfAuthoritiesCategories | TablesOfContents | TablesOfFigures | Variables | Versions | WebOptions | Window | Windows | Words | XMLChildNodeSuggestions | XMLNodes | XMLSchemaReferences

See Also | Templates Collection Object | Working with Document Objects