This Word VBA macro closes all documents.
Sub CloseAllDocuments() Dim doc As Document For Each doc In Documents doc.Close Next doc End Sub
Sub... Dim doc As Document
The 3rd line declares an object variable named doc that will refer to the Document object.
The Document object represents a single Word document.
Sub... Dim doc As Document For Each doc In Documents
The 5th line starts to loop through all the elements (Document) of a collection (Documents).
The [Application.]Documents property returns the Documents collection object.
The Documents property is a member of the Word.Global class, so we can omit the Application property.
The Documents is a collection of all the Document objects those are currently open in Word.
Closes Each Document
Sub... Dim doc As Document For Each doc In Documents doc.Close
The 6th line closes the document by the Document.Close method.
Sub... Dim doc As Document For Each doc In Documents doc.Close Next doc
The 7th line loops back to get the next document.
After every document is closed, this macro ends.
- Word 2013
- Word 2010
- Word 2007