During this hour you will learn
This lesson explores Visual Basic's OLE capabilities. If you've read much about the latest version of Visual Basic, you may wonder why a discussion of OLE appears in this tutorial before ActiveX. (Hour 31, "Creating a Simple ActiveX Control," begins ActiveX coverage.) The new ActiveX technology is supposed to replace OLE in Visual Basic version 5. OLE is still vitally important, however, because only some Visual Basic programmers will have access to ActiveX. Standard Edition VB programmers won't have access to primary ActiveX control creation and usage (except for a subset of ActiveX tools) and will have to rely on the OLE control if they want to embed outside objects in their applications. Also, although OLE is less powerful than ActiveX, OLE provides some groundwork learning before mastering ActiveX.
If all these OLE and ActiveX terms confuse you, read on. You'll soon see that these controls add capabilities that help eliminate coding, not increase your workload. OLE (which stands for Object Linking and Embedding) lets you reuse objects from other software applications. Therefore, you don't have to reinvent the wheel every time you need, for example, to add a painting program's features to your application. You'll be able to borrow from an existing program such, as Microsoft Paint.
FAST TRACK |
If you've used OLE in other applications, you'll appreciate the added power ActiveX gives your applications. See Hour 31, "Enhancing Your Program with ActiveX", for more information. |
Microsoft introduced Object Linking and Embedding a few years ago to take advantage of software reuse. By designing their applications according to the OLE standard, independent software vendors can create programs that fit seamlessly into Visual Basic and other Microsoft products. This strategy greatly increases programmer productivity by allowing the integration of these components into Visual Basic applications. OLE lets your applications connect to outside applications by using either linking or embedding technologies.
If you have Visual Basic's Standard Edition, you can still use ActiveX controls. When working with OLE controls, such as a Microsoft Excel worksheet, you actually are working with an ActiveX control if you're using Microsoft's latest version of Excel, from Office 97. The ActiveX technology works and acts like OLE technology, but ActiveX extends that technology, as you'll see in Hour 31.
The rest of this lesson sticks with the term OLE to teach the OLE control, but you'll know that some of the "OLE controls" you embed in your Standard Edition applications are really ActiveX controls. If you use the Professional or Enterprise editions, you'll be able to take ActiveX technology further than you can with OLE.![]()
Linking to an application means that your OLE control contains a link to another application's document (a data file). In fact, an OLE control is often called a container control because it holds an outside application's functionality. If the other application changes that document, your application will reflect the changes. The object is known as a persistent object because you can keep the object's contents up-to-date via the link.
Embedding a document means that your container control contains a document that was created by using an OLE-compliant application. It does not, however, maintain any links back to the external document. Therefore, if the outside application changes the external document, your application won't reflect that change.
When you place an OLE object on a form, you must make the linking or embedding decision. The primary difference between the two kinds of objects is that an embedded object appears to be part of the application (almost as though it were a custom control placed on the form); on the other hand, a linked object, when activated, calls up another program (the source program it's linked to). By using the source program, users can then work with the document's data. When finished, the user closes the source application and returns to the VB application.
This topic section explains more about what you can expect from an OLE object, and subsequent topic sections demonstrate OLE objects within applications.
An OLE Tutorial
The only way to change a document contained in your OLE control
is to open your application and activate the OLE control. Normally,
users do this by double-clicking the control itself. When the
OLE application appears, you can alter the contents of the document,
and then choose Close and Return to <VB Application>
from the File menu. Users then see the changed document
in the changed window. You'll see how this works throughout the
rest of this lesson.
Any object that supports Automation (or OLE Automation) is available as an OLE object. Many of today's Windows applications support Automation, so you'll often be able to use the OLE control to integrate your applications with other applications.
The OLE Container Control
The OLE Container control that you can double-click to add the control to the form (see Figure 22.1) holds your application's OLE object. You can link or embed as many OLE objects as your application requires by placing multiple OLE Container controls on your form. (Generally, simple applications contain only a single OLE Container control.)
The OLE Container control appears in the standard Toolbox.
The OLE Container control displays a rectangle on the form when you place the control. The control can display data from another application. You can resize the OLE Container control to adjust the size of the embedded or linked object.
When you place an OLE Container control on the form by double-clicking the Toolbox control, you don't see the OLE Container control rectangle immediately. Instead, VB displays the Insert Object dialog box (see Figure 22.2).
The Insert Object dialog box lets you describe the OLE object.
The initial Insert Object dialog box has the Create New
option button selected. This option lets you create a brand new
OLE object to place on the form.
If you inadvertently close the Insert Object dialog box, you can reopen it by right-clicking the OLE Container control and selecting Insert Object from the pop-up menu.
![]()
The Object Type list box contains the OLE-compliant application
controls you have to select from. The list varies according to
the software installed on your system. An application updates
your Windows Registry database with the application's OLE objects
when you install the application. Normally, one shrink-wrapped
Windows application can yield two, three, or more custom controls.
For example, if you have PowerPoint 97 installed, you'll find
two PowerPoint controls in the list.
The list box displays true controls. Your application can borrow these controls from the listed applications.
![]()
When you select the Create New option, VB embeds a generic
object into your OLE Container control. You then specify the kind
of object you want to embed.
If the OLE object already exists, you can click the Create fromFile
option button, which results in the Insert Object dialog box in
Figure 22.3.
You can select a current object to link or embed.
Notice the
Link check box below the Filetext box. If you mark theLink check box, Visual Basic links rather than embeds the object. However, when you create an object from scratch by using the CreateNew option button, the object is embedded in your application because you're creating the object just for your application.![]()
If you want to follow along throughout this lesson and add an
OLE object to an application, open a new project and double-click
the OLE Container control. Keep the Create New option selected.
Although you'll have a different set of applications installed on your system, this section will select the Windows Paint program's tools, which should always appear in the Insert Object dialog box. If you select Paintbrush Picture (Paintbrush is a synonym for Windows Paint) and click OK, Visual Basic closes the Insert Object dialog box and displays the object's rectangle in the center of your form, as Figure 22.4 shows.
Your application now displays a container for a Paint picture.
You have not imported an actual Paint picture-instead, you've inserted a mini-application version of Windows Paint directly inside your own application. Your own application will be able to communicate with the Paint object, but your users will be able to access Paint's capabilities when they run your application.
![]()
Look back at your screen if you followed the first topic's example. Where are your Visual Basic menu commands and toolbar buttons? Where did the form location and size coordinate section go? Where is Visual Basic's status bar? This topic section begins by explaining more about OLE objects and their properties. You'll then complete the simple OLE project that you began in the preceding topic section and see why the OLE Container control caused some of VB's screen, menu, and toolbars to go away.
The OLE Container control lets you create something called a document-centered application. The document, as alluded to in the first topic section, is actually data from one or more other applications. The OLE Container control holds the document you want to bring in from the outside application. This topic explains the OLE objects you link or embed in depth.
Linking and Embedding Revisited
One of the nice advantages of linking is that the data resides in only one location on your computer. No matter how many applications link to the object, such as a Microsoft Excel worksheet, the object takes up only a single location and consumes less disk space than if you embedded the object in the various applications.
Linking does pose its share of problems, however, especially in networked environments and in distributed applications. Users must have the source application that created the linked object, or they won't be able to modify the object. Also, if you want your application to reflect the linked object's current state (in case someone used the source application to modify the object after you link to the object), you must use the Update method to update the object, like this:
oleWorkSheetSales.Update
Without the Update method, the object retains the form and data contents that it contained when you first linked the object to your application. The updating might slow you application somewhat, because the update has to occur, but the object container will always be linked to the most recent version.
When you embed an object, the object appears frozen in time. Subsequent changes users make to the object from another application won't affect your Visual Basic application's copy of the embedded object.
Be aware of the increased file size that comes with embedded objects. Not only must Visual Basic store the embedded object with your application, but VB must also keep track of the source application that controls the object. (For example, Visual Basic can't manipulate an Excel worksheet, but Excel can; so Visual Basic has to remember the Excel connection to the object.)
![]()
One of the greatest benefits of OLE objects is that your application's users can double-click the object, and the running VB application starts the source application and lets users manipulate the control. Actually, the double-click is the default mechanism that starts the source application. You can change the OLE Container control's AutoActivate property to start the source application when the OLE Container control gets the focus. When your code triggers the object with the special VB DoVerb OLE Container control method, or when the object receives its normal activation event, the control gets the focus.
The Paste Special Object
If you want to use only part of an object, such as a selected range of cells from a worksheet, you can't use the Insert Object dialog box. Instead, you must use VB's Paste Special command available from the right-click menu. Paste Special uses the Windows Clipboard to make the transition from the original application's object to the OLE Container control.
This topic section's "Next Step" section explains how to use Paste Special instead of the Insert Object dialog box to insert part of existing objects into your application.
Runtime OLE
You can place an OLE Container control on your form without using the Insert Object or Paste Special dialog box to place the object in the container. The OLE Container control supports several methods, such as CreateLink and CreateEmbed, that let your VB code create a link at runtime. If your VB application is part of an overall system that uses worksheets, documents, and databases from other applications, your application may have to create a link to an OLE-compliant application at runtime after certain conditions are met.
Through coding, you can create linked and embedded objects at runtime. VB even includes methods that display the Insert Object or Paste Special dialog box at runtime so that your application's users can control the object coming into the application. Generally, you won't be writing such applications, and some VB programmers never have the need to let their users interactively select objects-but VB makes this option possible. This tutorial doesn't go into such programming logic because of how rarely most programmers need to display the Insert Object dialog box at runtime.
In the previous topic section, you included an OLE Container control in a project and created a new Paint picture object in the OLE Container control. The OLE Container control still looks like an empty rectangle, however. You'll add more to the OLE Container control in this example.
When you placed the OLE Container control and set up the container
to embed a Paint picture, the Visual Basic menus and toolbars
went away. You were left with a smaller set of menu items above
the Form window. If you were to click one of the menu bar options,
such as View, you may be surprised at what you see. Figure
22.5 shows that the View menu doesn't contain any familiar
Visual Basic options.
The View menu doesn't look like a VB View menu!
You aren't viewing the Visual Basic View menu; you're viewing
the Windows Paint View menu! (Sometimes, minor menu differences
between Visual Basic's object menus and the true source application's
menus appear.) If you move your mouse pointer to the OLE Container
control, the pointer changes to a drawing pencil. If you click
and drag the pencil inside the OLE Container control, you can
draw on the OLE Container control just as you would draw on the
Paint window. Figure 22.6 shows a simple line drawing inside the
OLE Container control.
You can draw something with Paint inside the Visual Basic Form window.
The OLE Container control won't expand to display the object you want to display. Change the OLE Container control's SizeMode property to 2 - AutoSize. SizeMode expands the OLE Container control to show the normal OLE document size. The drawing area won't be limited to the size of the OLE Container control that you used to place the control on the form.
![]()
Run your application to see what users see. Figure 22.7 shows the running application. Not much is really happening here, is it? A user (you in this case) sees the simple drawing that you made at design time. If you hadn't drawn the lines, users wouldn't know that the center of the application window even includes a Paint object except that they would see the blank rectangle. (You could have left the OLE Container's Paintbrush picture blank so users start with a clean slate.)
Add labels to describe objects; otherwise, users might not know what the object is for.
Obviously, you'll have to include labels and form text to let your users know what the objects you embed or link contain. Perhaps this application's form might be part of an engineer's application that tracks jobs, and the embedded Paint object lets the engineer make simple drawings that go with current building plans. You'll need to label the application and perhaps even write a user manual to let users know that they can activate the object.
When users activate the embedded object, the object's original purpose becomes obvious because the original application's menu and tools become available within your own application. Remember that you can change the object's AutoActivate property to change the activation requirement. In other words, you can change the object so that the object immediately activates (its application's menus and toolbars automatically appear) as soon as users bring the object into focus or at runtime.
Two forms of activation take place. In this case, you're seeing a separate window with the Paint application embedded within your OLE document. With a compound document (described in this hour's final topic section), the application's toolbars and menus actually change to Paint's, Excel's, or those of whatever kind of source application created the object.
![]()
You'll need to change one of the property values to activate the entire Paint program's tools and menu options. Close the application and click the OLE object to display its Properties window. Set the MiscFlags property to 2. The flags determine how the OLE object responds. When you set MiscFlags to 2 for the Paint object, Visual Basic starts Paint and places the embedded picture in the Paint window when the OLE object is activated.
Run the program, and you'll see nothing has changed since you ran the program previously. When you double-click the object, however, the application starts Windows Paint and places the object in the Paint window (see Figure 22.8).
Paint opens so you can edit the object.
Before you changed MiscFlags to 2, you could use Paint somewhat and edit the drawing in place, but you had no access to Paint's Toolbox and Color box. When the full Paint application begins, your users have complete access to these tools.
If you edit the object and close Paint's window by choosing Exit
and Return to <VB Application> from the File menu,
the changes you made will then be in place inside your application's
form. When you close the application, your changes aren't saved
with the application, however. You'll learn how to save changes
in the next topic section.
When you want to link or embed part of an object, such as part of an Excel worksheet, you must use the Paste Special OLE option. With Paste Special, you can create embedded and linked objects in your Visual Basic application.
If you have Excel, you can follow these steps to create an OLE object out of a partial Excel worksheet:
Get ready to paste the special Clipboard contents as an Excel object.
This topic section discusses some OLE extras that you'll need when working with OLE objects. For example, you may not want the embedded application's object to appear as a worksheet or as a picture but to appear as an icon. Users can double-click the icon and then start the source application. Also, you need to learn how to save changes so that the next time users run your application, the changes will reside with the application.
This topic wraps up this quick OLE discussion by describing some important properties and methods that may come in handy as you learn more about OLE's ActiveX replacement. Due to OLE's decrease in popularity now that the more powerful and extendible ActiveX controls have come out, you'll learn more powerful ways to accomplish OLE-like feats beginning in Hour 31 if you use the Professional or Enterprise edition of Visual Basic. If you use the Standard edition, you will, at the end of this topic section, understand OLE fundamentals that you'll be able to build on later after you master ActiveX. This topic provides the groundwork for ActiveX controls that you'll master in the future.
OLE as an Icon
Figure 22.3 shows the Insert Object dialog box. If you look back
at the figure now, you'll see the check box labeled Display
As Icon. If you click this option when you embed, link, or paste
special into an OLE Container control, Visual Basic doesn't display
the object's contents but instead displays an icon that represents
the object. Figure 22.10 shows the running Excel-based object
displayed as an icon (with a SizeMode property set to AutoSize).
An icon represents the Excel data.
The icon represents the application for the embedded object. When users double-click the icon, the application opens Excel and loads the original worksheet for editing.
You can change the OLE Container control's DisplayType property from 0 - Content to 1 - Icon at design time as long as you haven't embedded, linked, or pasted special an object into the control. After you change DisplayType to represent an icon when the application runs, you can embed or link an object to the control and its startup state will reflect the DisplayType entry.
![]()
In-Place Activation
One nice feature of some OLE objects is in-place activation. The running application's menus and toolbars change to the OLE object's source application's menus and support tools. Users don't feel as though they have left your application but that only the application's menus and tools changed. You'll learn about in-place activation in Hour 31 when you learn about ActiveX. Although OLE often supports in-place automation, the ActiveX controls provide better in-place activation support.
Saving Object Contents
When you make a change to a linked data item, the source application
makes the actual change because the object doesn't exist inside
your application. Therefore, you use Excel's or Paint's (or whatever
application's) Save command on the File menu to
save your changes. The changes will reflect in your application's
OLE object but not the next time you start the application unless
you issue the OLE Container control's Update method (such as in
the Form_Load() event procedure).
Microsoft recommends that you save and load changed embedded objects with several file-saving methods explained in the next example section. Your application is responsible for holding the embedded object, so the source application really has no jurisdiction over saving the object's content inside your application.
Use the SaveToFile method when saving your OLE Container control data. Suppose that you offer a command button to save changes made to an object. The following event procedure saves those changes in a special binary file. A binary file is more limiting than the sequential or random-access files that you learned about in Hour 21. Nevertheless, binary files provide a fast and efficient method for storing objects, as long as you later read objects in the same order you save them.
Here is code that saves your OLE Container control object:
Dim intFileNum as Integer ' Get next free file number intFileNum = FreeFile ' Open output file Open "TEST.OLE" For Binary As #intFileNum ' Save file oleObj1.SaveToFile intFileNum ' Close the file Close
The next time your application starts, the application should read the object's latest values (from the previous run) into the OLE Container control by using the ReadFromFile method as follows:
Dim intFileNum as Integer ' Get next free file numberintFileNum = FreeFile' Open input file Open "TEST.OLE" For Binary As #intFileNum ' Read file into object oleObj1.ReadFromFile intFileNum ' Close the file Close
As you learn more about OLE and eventually ActiveX, you'll need to master the following terms:
If you press Ctrl+T and click the Insertable Objects tab, Visual Basic displays a list of objects that you can insert into your applications. The object list comes from the Windows Registry that your applications update when you install them.
If you select one of the listed objects (such as the one labeled Microsoft Excel Worksheet) and click OK, Visual Basic updates your Toolbox as shown in Figure 22.11. Visual Basic actually inserted a new tool in your Toolbox that lets you insert Excel objects without going through the usual OLE Container control selection process from the Insert Object dialog box.
The in-place activation Excel OLE tool appears.
These tools provide for in-place activation, not just simple activation. When you embed an Excel object, for example, by clicking the Excel tool, you've inserted a special OLE object that supports in-place editing. If you were to add the Excel object tool to the center of a new project's Form window (assuming that you have Excel on your system) and then run the application, you would see the form in Figure 22.12 with a complete Excel worksheet in the middle of the Form window. All of Excel's functionality is now available, including Excel's primary menu options, even if the Form window didn't originally contain a menu.
Users can now use Excel from within your applications.
Generally, you'll want to offer in-place editing when it's available so that your users don't have to go outside the application to access the OLE data. You may not have the right in-place editing tool available (you'll find far more OLE objects in the Insert Object dialog box list than in the Insertable Objects dialog box), so you'll have to offer the next-best thing as described throughout the earlier sections of this lesson: the OLE Container control that supports activation of the source application and, possibly, also supports in-place editing if the OLE object's source application supports in-place activation.
You've only scratched the OLE surface. OLE offers a wide assortment of features that go beyond linked and embedded applications. Nevertheless, OLE is dying out and ActiveX is taking its place. ActiveX isn't fully dominant in the software development industry, so you may not have to make the switch to ActiveX soon, but someday you'll have to make that switch.
In the meantime, the OLE objects provide a starting location that lets you practice the same fundamental operations that you'll use with ActiveX. You can link, embed, and paste special objects, as well as create compound documents in which your users can access the object's tools and menus as though they were running the actual external application instead of one you wrote.
Next hour's lesson, "Enhancing Your Programs," starts a new part of the book, "Adding Power to VB Programs." You'll learn more Toolbox tools, as well as master the installation of new tools in your Toolbox.