CitectVBA Programming Reference > Understanding CitectVBA Language Basics > OLE Services > Returning an object

Returning an object

Most objects return a single object from the collection. For example, the Documents collection contains the currently open Word documents. You use the Documents property of the Application object (the object at the top of the Word object hierarchy) to return the Documents collection.

After you've accessed the collection, you can return a single object by using an index value in parentheses (this is similar to how you work with VBA arrays). The index value can be either a number or a name.

The following example uses the Documents property to access the Document collection. The index number is used to return the first document in the Documents collection. The Close method is then applied to the Document object to close the first document in the Documents collection.

objWordApp.Documents(1).Close

The following example uses a name (specified as a string) to identify a Document object within the Documents collection.

objWordApp.Documents("Sales.doc").Close

Collection objects often have methods and properties which you can use to modify the entire collection of objects. The Documents object has a Save method that saves all the documents in the collection. The following example saves the open documents by applying the Save method.

objWordApp.Documents.Save

The Document object also has a Save method available for saving a single document. The following example saves the document named Report.doc.

objWordApp.Documents("Report.doc").Save

To return an object that is further down in the Word object hierarchy, you must "drill down" to it by using properties and methods to return objects.

To see how this is done, in Word, open the Visual Basic Editor and click Object Browser on the View menu. Click Application in the Classes list on the left. Then click ActiveDocument from the list of members on the right. The text at bottom of the Object Browser indicates that ActiveDocument is a read-only property that returns a Document object. Click Document at the bottom of the Object Browser; the Document object is automatically selected in the Classes list, and the Members list displays the members of the Document object. Scroll through the list of members until you find Close. Click the Close method. The text at the bottom of the Object Browser window shows the syntax for the method. For more information about the method, press F1 or click the Help button to jump to the Close method Help topic.

Given this information, you can write the following instruction to close the active document.

objWordApp.ActiveDocument.Close SaveChanges:=wdSaveChanges

The following example maximizes the active document window.

objWordApp.ActiveDocument.ActiveWindow.WindowState = 
wdWindowStateMaximize

The ActiveWindow property returns a Window object that represents the active window. The WindowState property is set to the maximize constant (wdWindowStateMaximize).

The following example creates a new document and displays the Save As dialog box so that a name can be provided for the document.

objWordApp.Documents.Add.Save

The Documents property returns the Documents collection. The Add method creates a new document and returns a Document object. The Save method is then applied to the Document object.

As you can see, you use methods or properties to drill down to an object. That is, you return an object by applying a method or property to an object above it in the object hierarchy. After you return the object you want, you can apply the methods and control the properties of that object.

See Also

OLE Services