Chapter Three: Understanding Automation and Browser Architecture

Buy this book

Automation allows applications to communicate, exchange data, and control one another. Specifically, automation allows a client application to create and control an object, using the exposed object's interface. Automation is powerful because programmers can use this technology to access the functionality of any application that supports Automation. For example, if you were writing a mortgage calculation application, you could write the calculation engine yourself, but Automation gives you another option. Using Automation, you can access the calculation engine of Microsoft Excel and get Excel to do the calculations for you. In this way, developers can use the functionality of existing applications without recreating the same functionality in their own applications.

Software objects play an important role in Automation. For the purposes of this discussion, objects are nothing more than self-contained functional software components. Like objects in Microsoft Visual Basic, these components contain properties and methods. In fact, objects that are accessible through Automation behave just like the tools in the Visual Basic toolbox—if you know the properties and methods, you can easily use the component. The Microsoft Visual Basic for Applications language supports Automation by means of several keywords. In order to access an object, you use the CreateObject function. For example, the following code creates an instance of an Excel spreadsheet from Visual Basic:

Dim objWorksheet as Object
Set objWorksheet = CreateObject("Excel.Sheet") 

In general, the syntax for the CreateObject function is

CreateObject("ServerName.Class")

where ServerName is the name of the application supplying the object and Class is the type of object you want to create. Once the object is created, the program can use any of the exposed properties and methods defined for the object. For example, Listing 3-1 shows how a Visual Basic program can access the spell checking method of an Excel worksheet object through Automation. Figure 3-1 shows a sample of the output from the program in Listing 3-1. Note that Listing 3-1 and all other full program listings are available on this book's companion CD in the samples folder.

`This demo uses the spell checking capabilities
`of Microsoft Excel.
`This is accomplished by accessing the CheckSpelling
`method of Excel using Automation.
Private Sub Command1_Click()
    Dim objWorksheet As Object
    Set objWorksheet = CreateObject("Excel.Sheet")
    objWorksheet.Cells(1, 1).Value = Text1.Text
    objWorksheet.CheckSpelling
    Text1.Text = objWorksheet.Cells(1, 1).Value
    Set objWorksheet = Nothing
End Sub 

Listing 3-1.

Using Excel's spell checker through Automation.

Figure 3-1.

Using Excel's spell checker.

When using Automation to develop new applications, programmers are often frustrated by the lack of documentation describing the functionality of various software objects that can be used in an application. When object documentation is provided, manufacturers typically supply it in the form of an object model. An object model is a hierarchical representation of the objects contained in an application and their interrelationships.

Figure 3-2 shows a partial Excel object model. Excel actually contains dozens of objects, but the partial model clearly shows the relationships among some of the objects. For example, Excel is presented in a workbook/worksheet paradigm, which is represented by the Workbook and Worksheet objects. The relationship between a workbook and a worksheet is indicated by the placement of the objects in the hierarchy.

Figure 3-2.

A partial Excel object model.

KEY CONCEPT: Object models are used to describe various objects in an application, as well as their relationships.

Another good source of object information is the object browser. The object browser is an application that presents objects, properties, and methods in a dialog box. Visual Basic has a built-in object browser that can be used to view object information. Figure 3-3 shows the Visual Basic 4.0 Object Browser displaying the Microsoft Internet Explorer version 3.0 object, along with the corresponding methods and properties. These properties and methods are discussed in detail later in this chapter. Like Excel objects, Internet Explorer objects can be used directly from Visual Basic. If you need more information on any object model, contact the manufacturer of the application. Manufacturers should be able to provide a complete Automation object model.

Figure 3-3.

The Visual Basic 4.0 Object Browser.

© 1996 by Scot Hillier. All rights reserved.

Show: