![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Lets look at an example for which you might actually use OLE automation. Imagine you are writing an application for a stock market analyst who needs to download market data from an online service, perform calculations on the data to come up with various economic predictions, then email the resulting report to a list of clients. Using Visual Basic alone, this presents a formidable programming job. With OLE automation, however, the job becomes much easier:
You can use OLE automation by itself or in conjunction with linking or embedding. If you link or embed a programmable object, you have the option of sending it commands using OLE automation or in-place activation (or both). The method you select will depend on the needs of the project. As a Visual Basic programmer, you can think of programs that support OLE automation as being a kind of custom control. The functionality that these programs expose through the OLE interface are control methods, and the program variables and settings that control its operation are its properties. Creating An OLE Automation ObjectAs with embedded OLE objects, you can create a new OLE automation object or open an existing one based on a disk file. In a Visual Basic program, an OLE automation object is identified by a variable of type Object. The first step in creating an object, therefore, is to declare a variable of this type: Dim MyObject As Object Next, create a new, empty OLE automation object by using the Set statement with the CreateObject function. This function has only one argument, specifying the application name and the class name of the object to be created. For example, the statement Set MyObject = CreateObject (Word.Basic) creates an object based on the Microsoft Word application and the Basic class. You must provide both the application name and class name, because some applications expose more than one class of object. To create an OLE automation object based on an existing file, use Set with the GetObject function, as shown here: Set MyObject = GetObject(c:\data\finance.doc) GetObject can accept a second, optional argument specifying the object class name. This argument is required only if the specified file contains two or more object types. An Excel spreadsheet file, for example, can contain both Sheet and Chart objects. To specify the Sheet object in an Excel file, you would write Set MyObject = GetObject(C:\WORKSHEETS\ANNUAL.XLS, Excel.sheet) where Excel is the server application name and Sheet is the object type. Not all Windows applications expose OLE automation objects. You can be confident that applications from Microsoft do, but other software publishers differ in their degree of support for this technology. I expect that the list of objects will grow rapidly as other software publishers adopt the OLE automation standard. An applications documentation or online help should provide information about the objects it exposes and the methods and properties associated with them. Just like different Visual Basic controls, objects exposed for OLE automation differ in their capabilities. A Word.Basic object, for example, does not permit access to the contents of a document using OLE automation, unless the object is also embedded in an OLE Container control. An Excel.Sheet object is different, because it does not need to be embedded for us to use OLE automation for accessing its contents. Obtaining Object InformationVisual Basic provides two tools for finding information about the objects available on your system. These tools are extremely useful, because a big problem when working with OLE automation is determining the details of the available objects, their methods and properties, and how to use them. For a separate application program, the best source of information is usually the programs own documentation or help system. For online information within Visual Basic, you can use the References list and the Object Browser. You can display the References list by selecting References from the Visual Basic Project menu. This dialog box was discussed in Chapter 6 and is shown again in Figure 10.6. The list displays all of the object sources, or libraries, that are registered on the system. The ones with a checkmark in the adjacent box are available in the current Visual Basic project. You can add or remove a library from the project by clicking on its box. Because a typical system has many available object sources, loading them all into every Visual Basic project would not be efficient. By selecting only the ones you need, you minimize memory use and maximize speed. If you try to remove a library that is in use, Visual Basic displays a warning message. An object library that is selected in the References list does not necessarily mean that the library is used by the projectonly that it is available. When Visual Basic encounters a reference to an object in our code, it searches the object libraries selected in the References list in top-to-bottom order for a matching object. If two or more applications or libraries expose objects with the same name, Visual Basic uses the first one it finds. Deselecting unused object libraries in the References list will speed up Visual Basics search process, because it has fewer libraries to search. You can also use the Priority buttons in the References dialog box to move frequently used libraries to the top of the list for additional speed improvement.
Visual Basic will not permit you to deselect a library that is used in the current project. This includes libraries providing objects that are referenced explicitly, as well as libraries used by all custom controls that are installed using the Custom Controls command on the Tools menu. You can never delete the Visual Basic for Applications and Visual Basic Objects and Procedures references; these are necessary for running Visual Basic. The References list shows which object libraries are available, but it tells you nothing about the objects in those libraries. For this, you need the Object Browser, displayed by pressing F2 or selecting View Object Browser. This dialog box is shown in Figure 10.7. The dialog box is described here:
Note the use of different icons in the Class Browser to identify different types of items. In the Classes list, Classes, Enums, and Modules each has its own icon. Likewise, in the Members list, Properties and Methods have unique icons. When you click on an item in the Classes or Members list, the panel at the bottom of the Object Browser displays information about the item.
|
![]() |
Products | Contact Us | About Us | |