You create OLE container applications by using OLE custom controls, linked and embedded objects, and object applications. This chapter tells you how to use Visual Basic's tools to assemble these OLE building blocks.
In this chapter, you learn how to do the following:
Visual Basic 4.0 introduces a whole new type of control: the OLE custom control. OLE custom controls (.OCX) look and work the same as old-style custom controls (.VBX). In fact, Microsoft provides tools to third-party developers to help them convert their .VBXs to .OCXs.
The standards on which .VBXs were built were written specifically and (at the time) exclusively for Visual Basic. Since then, other development tools, such as Microsoft Visual C++ and Borland C++, have added support for .VBXs. The differences between these development environments and Visual Basic are significant, so the .VBX standard no longer suffices for all custom controls.
Additionally, Visual Basic is now available on several different operating systems�Windows 3.1, Windows NT, and Windows 95 (Chicago). Soon, Visual Basic might also be available on the Macintosh and other platforms. The .VBX standard was written for Windows 3.x and took advantage of features that change under the new versions of Windows. Rather than invest a lot of effort in maintaining the .VBX standard on all platforms, Microsoft (wisely) decided to leverage the work that was already being done by its OLE developers.
The OLE standard is much broader than the original .VBX standard. This broader scope enables such applications as Microsoft Access and Excel to incorporate custom controls. In all, the .VBX to .OCX evolution is an inevitable step in the onward march of computer technology.
The advantages of OLE custom controls over .VBXs might not be immediately obvious. For example, OLE custom controls are not faster than .VBXs, nor do they require less memory. Instead, OLE custom controls promise these future advantages:
Although none of these advantages provides an immediate, tangible benefit today, they are almost certain to provide a return on your investment in the near future.
For instance, it is easy to imagine tools that might run in the background maintaining the associations between the system registry and the custom controls that you've installed. If you move a file to a new directory, the tool might update REG.DAT automatically. Such a tool might be included with Windows 95 or be developed by some enterprising reader looking to make it big.
OLE custom controls appear and behave nearly identically to old-style .VBX controls. However, Visual Basic 4.0 incorporates several new features that change the way that you work with custom controls:
The following sections describe these tasks in greater detail.
To draw a custom control on a form, follow these steps:
Drawing a custom control on a form is the same as drawing any other control�you simply click and drag.
To add an OLE custom control (.OCX) to the Toolbox, follow these steps:
The Custom Controls dialog box.
Visual Basic adds custom controls to the Toolbox.
If You're Having Trouble
If a custom control appears on the Available Controls list of the Add Custom Control dialog box but does not appear on the Toolbox when you select the control, check whether the file that contains the control (.OCX) is still available in your Windows system directory (usually \WINDOWS\SYSTEM). The file might have been moved or deleted. The locations of custom controls are maintained in the system registry and if files have been moved or deleted, the path recorded in the system registry might not agree with the actual path on your hard disk.
To fix this problem, copy the custom control file to the Windows system directory.
The Insertable Objects and Controls check boxes on the Custom Controls dialog box (see fig. 12.2) enable you show or hide either type of OLE object in the Available Controls list box. If you have many applications installed on your system but don't often use objects from those applications, you might want to select only Custom Controls. This choice makes it a little easier to find the custom control that you want.
Normally, Visual Basic adds a custom control to its list of available controls when you install the control. If you do not properly install a control, it might not appear in the Available Controls list on the Add Custom Control dialog box.
To add a control to Visual Basic's list of available controls, follow these steps:
Choose Browse in the Custom Controls dialog box to display the Add Custom Control dialog box, in which you can add a custom control to the list of available custom controls.
One of the advantages that OLE custom controls have over .VBX controls is that you can view control methods and procedures by using the new OLE Object Browser.
To view the methods and properties for a OLE custom control, follow these steps:
The Object Browser displays the methods and properties of any OLE custom control, object, or library of objects.
The Object Browser lists the syntax and a brief description of the properties and methods of all OLE objects.
The Object Browser does not list the events an object supports. To add event procedures for a custom control, use the Object and Proc list boxes on the Visual Basic Code window (see fig. 12.7).
Use the Code window to view the events that an OLE custom control supports.
Visual Basic 4.0 ships OLE custom controls that replace all the .VBX controls included in Visual Basic 3.0 Professional Edition. The version 3.0 controls still work with Visual Basic 4.0, but if you want to run your applications on 32-bit operating systems, such as Windows NT or Windows 95, you should upgrade those projects to use the new OLE custom controls.
Many other custom control vendors also provide OLE custom controls to replace their older .VBX controls. You might want to contact your custom control provider before you begin upgrading your projects�it is easier to perform the upgrade for all controls once instead of upgrading a project in a piecemeal fashion.
To upgrade a project containing .VBX controls to use new OLE custom controls, follow these steps:
The Custom Control Upgrade dialog box appears before Visual Basic converts .VBX controls to .OCX controls in a project.
Visual Basic 4.0 files are not backwardly compatible with earlier versions.
If you are upgrading your project from Visual Basic 3.0, Visual Basic displays another warning (see fig. 12.10). Data access objects are OLE objects in Visual Basic 4.0, not objects built in to the language. Therefore, projects that use data access objects must reference the Microsoft Data Object Library. Click Add if your project uses data access; otherwise, click Don't Add.
If you ask it to, Visual Basic will add a reference to the Microsoft Data Object Library.
Visual Basic doesn't upgrade .VBX controls that lack equivalent OLE custom controls installed on your system. The setup program written by the custom control provider handles the installing and registering of the mapping between .VBX controls and OLE custom controls. If you have problems upgrading a control, you might want to contact the custom control provider to see whether it knows of the problem and offers special procedures to use when upgrading their custom controls.
Not all .VBX controls have .OCX equivalents. Before planning any migration to Windows 95 or Windows NT 3.51, check with your custom control vendor to make sure updated controls are available.
In Visual Basic 3.0, you had to use the OLE 2.0 custom control (MSOLE2.VBX) to add linked or embedded objects to a form. In Visual Basic 4.0, you can add embedded objects directly to the Toolbox and then draw them on the form, just as you would any other control (see fig. 12.11).
Add insertable OLE objects to the Toolbox, then draw them directly on a form.
Visual Basic refers to these embedded OLE objects as insertable objects throughout its documentation.
The Setup program doesn't install embedded objects in the Toolbox because they are provided by OLE applications, such as Microsoft Word and Excel, that don't ship with Visual Basic.
To add an embedded object to the Toolbox, follow these steps:
The Custom Controls dialog box lists the insertable OLE objects as well as available custom controls.
An Excel worksheet embedded on a form.
The Insertable Objects and Controls check boxes on the Custom Controls dialog box (see fig. 12.12) enable you to show or hide either type of OLE object in the Available Controls list box. You might want to select only Insertable Objects if you are not adding custom controls to your Toolbox. This makes it a little easier to find the object that you want.
To draw an embedded object on a form, follow these steps:
Drawing an embedded object on a form is the same as drawing a control�simply click and drag.
Embedded objects need not support in-place editing. If an object supports in-place editing, Visual Basic starts the application that provides the object and displays the object's toolbar or menu on the form, as shown in figure 12.15.
The Microsoft Excel Chart object supports in-place editing, so it displays the charting menu right on the form.
If an object does not support in-place editing, the application that provides the object starts up when you release the mouse button.
Table 12.1 lists widely installed applications, their embedded objects, and whether they support in-place editing.
Table 12.1 Many Applications Now Provide Embedded Objects, but Only Some Support In-Place Editing
Application | Version | Object | Supports In-Place Editing (Y/N) |
---|---|---|---|
Microsoft Access | 7.0 | Database | Y |
Microsoft Excel | 7.0 | Worksheet | Y |
Chart | Y | ||
Graph | Y | ||
Microsoft Excel | 5.0 | Worksheet | Y |
Chart | Y | ||
Graph | Y | ||
Microsoft Equation | 2.0 | Equation | Y |
Microsoft Graph | 3.0 | Graph | N |
Microsoft PowerPoint | 7.0 | Presentation | Y |
Slide | Y | ||
Microsoft Windows | 3.1 | Package | N |
Paintbrush Picture | N | ||
Sound | N | ||
Microsoft Windows | 95 | Package | N |
Sound | Y | ||
Paintbrush Picture | Y | ||
Video clip | Y | ||
WordPad Document | Y | ||
Shapeware Visio | 3.0 | Drawing | Y |
Shapeware Visio | 2.0 | Drawing | N |
Microsoft Word | 2.0 | Document | N |
Microsoft Word | 6.0 | Document | Y |
Picture | Y | ||
Microsoft Word | 7.0 | Document | Y |
Picture | Y | ||
Microsoft WordArt | 2.0 | WordArt | Y |
When you draw an embedded object on a form, the object appears without data. You can enter new data directly in the object at that time. If you select another control on the form, the embedded object is closed. To reopen an embedded object for editing, follow these steps:
An embedded Excel worksheet object displaying its pop-up menu.
Drawing embedded objects on forms using the Toolbox button for the object works great for new objects that don't yet contain data. However, moving the contents of an existing file into one of these embedded objects is awkward; you must open the object in its application, then cut and paste the data into the file.
It is easier to create an embedded object from a file by using the OLE 2.0 control. To create an embedded object from a file, follow these steps:
Use the Insert Object dialog box to create an embedded object from a file.
Type the file name or choose Browse to specify a file to use.
An embedded object created in this way is a copy of the data in the file�it is stored and updated separately from the original file. To ensure that an object is updated and kept in sync with its source file, use a linked object rather than an embedded one.
In Visual Basic 3.0, linking and embedding was provided through the OLE 2.0 custom control (MSOLE2.VBX). In Visual Basic 4.0, the OLE 2.0 control is built in. The OLE 2.0 control works the same way that it did in Visual Basic 3.0�you can still use it to add linked or embedded objects to forms.
When adding embedded objects, it is easier use the Toolbox. For more information, see "A New Way to Insert Embedded Objects," earlier in this chapter.
To draw a linked object on a form, follow these steps:
Use the Insert Object dialog box to add linked objects to a form.
Type the file name or click Browse to specify a file to use.
Unlike embedded objects, linked objects never support in-place editing. When you edit a linked object, the object's application always starts up in another window and loads the linked file.
To edit a linked object on a form, follow these steps:
An Excel worksheet object displaying its pop-up menu.
Editing a linked worksheet file with Excel.
An OLE object might look different in design mode or run mode, as shown in figure 12.23.
In design mode, Visual Basic updates the linked object as the file changes. In run mode, the object is grayed until the file is closed.
In Run mode.
Linked and embedded objects are no longer the only type of OLE objects that you can use in Visual Basic. Object applications differ from linked and embedded objects in that you can't place them on a form. Instead, object applications can display their own dialog boxes and forms to perform services, such as access to databases or retrieving a password from a user. Figure 12.24 shows the differences between linked and embedded objects and object applications.
Linked and embedded objects appear on a form. Object applications might display their own forms or run invisibly in the background.
You can create object applications in Visual Basic or Visual C++. Part III, "Creating OLE Objects," teaches you how to create your own object applications using Visual Basic. This chapter is concerned only with using those applications after they exist.
However, to show you how to use an object application, you must have one installed on your system. The easiest way around this catch-22 is to go ahead and build one of the sample applications that ships with Visual Basic: GRAVITY.VBP.
To build GRAVITY.VBP, follow these steps:
Compile the GRAVITY.VBP sample file to see how to use an object application.
You use the Object Browser to view the objects, properties, and methods that an object application contains. However, before you can browse an application object, you must first establish a reference to that application. It's not absolutely necessary to establish a reference before using an object application, but doing so does enable the Object Browser and provides syntax checking for properties and methods.
To view GRAVITY.EXE's objects, methods, and properties, follow these steps:
The References dialog box establishes a reference between the open project and object applications or object libraries.
GRAVITY.EXE contains one object and three methods or properties.
You can use the objects, methods, and properties listed for GRAVITY anywhere in code. Unlike other types of objects, however, object applications do not provide events that you can use in Visual Basic.
If You're Having Trouble...
If you are having trouble establishing a reference to an object application, run the object application and then try again. Object applications check their system registry entries every time that they start up. Often this fixes the problems in the system registry.
Use the CreateObject and GetObject functions to run an object application. CreateObject and GetObject use this syntax:
The placeholder objVariable is an object variable that contains the object that CreateObject and GetObject return. This is the variable used throughout your code to refer to the object application.
The placeholder programmaticID is the name of the object application as it is entered in the system registry. This name has the form ApplicationName.ObjectName. For example, excel.application or gravity.ball.
CreateObject and GetObject both return an object from the application, as shown in the following table:
Use | To | Example |
---|---|---|
CreateObject | Start a new copy of the object application | Set objBall = CreateObject ("gravity.ball") |
GetObject (first argument null) | Get a running instance of the object application | Set objBall = GetObject (" ","gravity.ball") |
GetObject (first argument provided) | Get a specific object from the object application | Set xlsExcel = _GetObject ("c:\excel\stock.xls","excel.sheet") |
To run the GRAVITY object application, follow these steps:
Option Explicit
' Declare an object variable for the object application.
Dim mobjBall As Object
The variable mobjBall is declared at the form level, so other procedures in the form can use it. This ensures that the object variable is still available after the Form_Load event creates the object in the next step.
Private Sub Form_Load() ' Run the object application ' by calling CreateObject. ' CreateObject returns the object ' from the application. Set mobjBall = CreateObject("gravity.ball") End Sub
The CreateObject line starts GRAVITY.EXE, returns the Ball object, and assigns it to the object variable mobjBall. You must use Set when assigning object variables.
One of the hardest parts about using CreateObject and GetObject is finding the right programmatic ID for the object application that you want to start. Table 12.2 lists some commonly installed applications and the programmatic IDs for their objects.
Table 12.2 Many Applications Provide Objects, but Their Programmatic Ids Are Often Hard to Find
Application | Object | Programmatic ID |
---|---|---|
Microsoft Excel | Application Worksheet Chart |
excel.application excel.sheet excel.chart |
Shapeware Visio | Application | visio.application |
Microsoft Word | Basic | word.basic |
The programmatic ID of an object application created in Visual Basic consists of the application name and the object name, as in gravity.ball or calc.application.
The application name is the name entered in the Project Name text box of the Project tab of the Options properties pages. The object name is the name of the class (see fig. 12.28).
For object applications created in Visual Basic, the programmatic ID consists of the application name and the class name of the object.
After starting an object application as described in the previous section, you can use the object's methods and properties just as you would with any other Visual Basic object or control.
To see how this works with the GRAVITY.EXE object application, follow these steps:
The Ball object's Throw method starts GRAVITY.EXE tracking the trajectory of a hypothetical ball influenced by the earth's gravity.
The Ball object's Distance and Height properties return the coordinates of the hypothetical ball that was thrown in step 1.
The application that you create in these steps demonstrates how to use the Throw method and Height and Distance properties of the Ball object provided by GRAVITY.EXE.
The application shown in this section isn't terribly exciting. You can improve on the application by moving the option button to follow the ball's trajectory on the form. See the sample GRAVTEST.VBP in the \SAMPLES\OLESERV directory for more ideas.
After you start an application by using CreateObject or GetObject, it continues running as long as one or more object variables refer to an object in the application. This principle is fundamental to OLE.
OLE keeps track of the number of references to an application. If the application is not visible and the count goes to zero, OLE closes the application. If the application is visible and the count goes to zero, OLE leaves the application running.
To see how this works with the GRAVITY.EXE object application, follow these steps:
Setting the object variable mobjBall to Nothing changes the reference count of GRAVITY.EXE to zero. Because GRAVITY does not have a visual interface, OLE closes the application.
Applications that have a visual interface usually provide a Quit or Exit method to close. For example, these lines start Microsoft Excel, then close that application:
Not all applications follow the OLE standard, however. Most notably, Microsoft Word closes when no longer referenced, even if it is visible. These lines start Microsoft Word 6.0, then close the application:
In this regard, Word is not a "well-behaved" OLE application. In all cases, it is important to experiment with object applications to determine how well they follow the OLE standards.
Each OLE object installed on your system has one or more entries in the system registry. One of the best tools for exploring the OLE objects on your system is the Registry Editor (REGEDIT.EXE). This tool ships with Windows and comes installed in the Windows directory.
To explore your system's registry, follow these steps:
The Registry Editor lets you view the entries in your system's registry.
Use the Find dialog box to search for insertable objects, programmatic IDs, or applications by name.
The listing that appears in the Registry Editor is confusing at best. Table 12.3 describes what some of the items mean.
Table 12.3 The Meanings of Some of the Keys in the System Registry
Key Item | Meaning |
CLSID | The item's class ID, a number that identifies the object to OLE, and that is used by the OLE .DLLs when referring to the object. |
insertable | An indicator that this object supports in-place editing for embedded objects. |
notinsertable | An indicator that this object does not support in-place editing for embedded objects. |
progid | The programmatic identifier used to start the application as an application object. You cannot use all objects with programmatic identifiers CreateObject or GetObject, however. You can't readily tell which objects are creatable by looking at the system registry. |
server | A list of the path and file specification of the application that provides the object. |
verb | The menu items that this object adds to the pop-up menu when right-clicked. Typical actions are Edit and Open, but some objects may support additional verbs, such as Play. |
For more information on the following topics, see the indicated chapters:
© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.