Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Both embedded objects and linked objects originate in the server application that exposed it. Consider a Microsoft Excel spreadsheet object. Whether linked or embedded, the object becomes a part of the container application, but in different ways:

  If you link the spreadsheet object, the container document contains links to the spreadsheet file, as well as a visual image of it for display purposes. The spreadsheet data itself is stored in the original Excel spreadsheet file, just as any other spreadsheet. If that spreadsheet file is modified, the container document will display the changes. The container application saves the link information and an image of the data object, but does not save the actual data.
  If you embed the spreadsheet object, the data is included as part of the container document. If the embedded data originated in an existing spreadsheet file, it becomes totally separate and independent from the original file. If the original file is modified, the container does not reflect the changes. The container can, however, use the server application to modify its own copy of the object. It is the container application’s responsibility to store the data on disk.

Obviously, whether you use linking or embedding depends upon the specific situation. Linking permits data from a single source to be automatically updated in one or more container applications that depend on it. This sounds similar to DDE, but OLE has an advantage over DDE: An OLE container has quick and direct access to the server application for modification of the linked data. Embedding is not useful for automatic updating of data, because only one application (the container) has access to the data. You use embedding when you want to provide a single application (the container) with access to the data manipulation and presentation abilities of a variety of server applications.

At the risk of generalizing, DDE is most suitable when program code needs access to the data; OLE is preferable when you need only to display the data and edit it using the original application. Like all generalizations, exceptions surely exist, but this generalization serves as a pretty good guide.


TIP:  In-Place Activation

The new OLE standard supports in-place activation, which permits you to edit an OLE object within the container application, as described earlier. With previous versions of OLE, activating an object caused the server application to start in its own window with the activated object displayed for editing. After completing the necessary editing tasks, you closed the server and were returned to the container application with the modified object displayed.


OLE Containers In Visual Basic

One of the delights of Visual Basic programming is that previously complex tasks are rendered easy or even trivial. Implementing an OLE container is no exception. You let a control—in this case, the OLE Container control—do the work for you. This control has many properties that you have already used with other controls, such as those controlling its size and visibility. Several specialized properties also relate specifically to OLE, which we will cover in this section.


TIP:  Visual Basic Programs As OLE Servers

A Visual Basic program can be a server, too—but it’s not called OLE. Using ActiveX technology, as described in Chapter 8, a Visual Basic program can expose objects for use by other programs.


The Windows Registry

For a Visual Basic program—or any program, for that matter—to act as an OLE container, it must know which OLE server applications are available. A container application cannot assume that a particular server application is available. Information about OLE servers is kept in the Windows registry. This information is maintained automatically. When an OLE server is installed, part of the installation process consists of registering the server by placing certain information in the registry. This information includes the server command line, the OLE protocols it supports, the class names of the objects it supports, and so on. This, of course, is exactly the information required by any program trying to establish itself as an OLE container. By obtaining this information from the registry, a container application can be sure that it will attempt to establish an OLE relationship only with servers that are available. A Visual Basic program uses the OLE control to access the registry.


TIP:  The Deleted Servers Problem

Problems can arise if an OLE server is deleted by simply erasing its program files. Its registration information will still be present in the registry, so container applications may still try to establish an OLE relationship with it. The result, of course, is an error. Whenever possible, use an application’s setup program to uninstall it. You can also use the Windows Add/Remove Programs utility. Properly designed uninstall programs remove not only the program files, but also the registry entry, preventing future OLE errors.


Inserting An OLE Object

The OLE control permits an OLE object to be inserted during program design (by the programmer) or while the program is running (by the user). Of course, the program must include the code for the latter action. At runtime, the OLE process can be controlled by the user working with dialog boxes or by program code setting properties of the OLE control. Depending on the circumstances, one of these methods will be more appropriate.

Inserting an object into an OLE container can be accomplished three ways. These three methods apply to both linking and embedding:

  Paste the object into the container using the Paste Special command (or equivalent ). This is possible only if a valid OLE object is on the Windows Clipboard. This object must have been copied to the Clipboard by the server application—for example, a range of cells in an Excel worksheet. The Paste Special command lets us embed the object with the Paste option or link the object with the Paste Link option. For certain types of objects, only the Paste option is available.
  Create a new object, a blank “whatever-it-is” you’re embedding (linking is not possible with this method ). Specify the server application, which will start (in-place, if supported). Then use the server’s tools and commands to create the object and switch back to the container application.
  Insert the object from an existing file, a data file belonging to the server application. Specify both the file name and whether the object is to be linked or embedded.

You can use these three methods of inserting OLE objects at both design time and runtime.

Inserting An Object At Design Time

If you insert an OLE object at design time, the link is fixed to the specified object and cannot be switched to another object while the program is running. For example, if you want a specific Excel workbook linked to or embedded in your Visual Basic application each and every time it runs, you would use this approach.

To insert an object during program design, place an OLE control on the form. When you do so, Visual Basic will display the Insert Object dialog box, as shown in Figure 10.2. If you want to create a new, blank object, select the type of object from the list, then click on the OK button (remember, new objects can only be embedded, not linked). The server application associated with the selected object will start, and a blank object will display in the OLE control—or in its own window if you selected the Display As Icon option. Use the application’s tools and commands to create and edit the data, or leave the object blank. The server application’s menu is displayed—without the File menu. Because the data object is embedded and will be stored within the OLE container, you have no need for the File menu commands.

The exact details depend on the specific server. If you insert an Excel spreadsheet, for example, a grid of cells displays in the OLE control and the Excel menu appears at the top of the Visual Basic form. In any case, you can click on the form outside the OLE control to return to Visual Basic program design.

If you want to insert an object from an existing file, select the Create from File option in the Insert Object dialog box. The dialog box will change, as shown in Figure 10.3. Enter the name of the desired file in the File box, or click on the Browse button to select the file. Click on the Link option to link the object; otherwise, the object will be embedded. Click on the OK button, and the selected object will be displayed in the OLE control. If you select an invalid file—one that is not associated with a registered OLE server—an error message will be displayed.


Figure 10.2  The Insert Object dialog box is displayed when you place an OLE Container control on a form.


Figure 10.3  The Insert Object dialog box also permits you to create an object from an existing file.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.