Visual Basic Concepts
Working with Externally Creatable and Dependent Objects
How you create a reference to an object provided by a component depends on whether the object is an externally creatable or dependent object. You can directly create a reference to an externally creatable object; you create a reference to a dependent object indirectly by using a method of a higher-level object in the component's object hierarchy.
Externally Creatable Objects
Most large ActiveX-enabled applications and other ActiveX components provide a top-level externally creatable object in their object hierarchy that:
Provides access to other objects in the hierarchy.
Provides methods and properties that affect the entire application.
For example, the Microsoft Office applications each provide a top-level Application object. The following example shows how you can assign references to the Application objects of Microsoft Excel, Microsoft Word, and Microsoft Access:
Dim xlApp As Excel.Application
Dim wdApp As Word.Application
Dim acApp As Access.Application
Set xlApp = New Excel.Application
Set wdApp = New Word.Application
Set acApp = New Access.Application
You can then using these variables to access the dependent objects in each application and the properties and methods of these objects. For more information see "Creating a Reference to an Object."
Note The Excel.Application syntax for referring to the Microsoft Excel Application class is not supported in versions prior to Microsoft Excel 97. To refer to the Microsoft Excel Application class in Microsoft Excel 5.0 and Microsoft Excel 95, use the syntax [_ExcelApplication] instead. For example:
Set xlApp = New [_ExcelApplication]
In addition to these top-level externally creatable objects, ActiveX components can also provide externally creatable objects that are lower on the component's object hierarchy. You can access these objects either directly as an externally creatable object or indirectly as a dependent object of a higher-level externally creatable object. For example, you can create a reference to a DAO TableDef object either directly or indirectly:
' Create a reference to daoTable1 directly.
Dim daoTable1 As DAO.TableDef
Set daoTable1 = New DAO.TableDef
daoTable1.Name = "Table1"
' Create a reference to daoTable2 indirectly,
' as a dependent object of the DAO DBEngine object.
Dim daoDBE As DAO.DBEngine
Dim daoWs As DAO.Workspace
Dim daoDb As DAO.Database
Dim daoTable2 As DAO.TableDef
Set daoDBE = DAO.DBEngine
Set daoWs = daoDBE.Workspaces(0)
Set daoDb = daoWs.CreateDatabase("db1.mdb", _
dbLangGeneral)
Set daoTable2 = daoDb.CreateTableDef("Table2")
Some objects provide an Application object, but give it a different name. For example, the Microsoft Jet database engine in Microsoft Access calls its top-level object the DBEngine object.
Dependent Objects
You can get a reference to a dependent object in only one way — by using a property or method of an externally creatable object to return a reference to the dependent object. Dependent objects are lower in an object hierarchy, and they can be accessed only by using a method of an externally creatable object. For example, suppose you want a reference to a Button object from Microsoft Excel. You can't get a reference to this object using the following code (an error will result):
Dim xlButton As Excel.Button
Set xlButton = New Excel.Button
Instead, use the following code to get a reference to a Button object:
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlButton As Excel.Button
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
Set xlButton = xlSheet.Buttons.Add(44, 100, 100, 44)
' Now you can use a Button object property.
xlButton.Caption = "FirstButton"
Figure 10.5 illustrates how a Visual Basic application gets a reference to the Button object.
Figure 10.5 Accessing dependent objects