Contrasting Linked and Embedded Objects

See Also

You use a component's visual interface to contain data from another application by linking or embedding that data into your Visual Basic application. The primary difference between a linked and embedded object is where their data is stored. For example, data associated with a linked object is managed by the application that created it and stored outside an OLE container control. Data associated with an embedded object is contained in an OLE container control and can be saved with your Visual Basic application.

When a linked or embedded object is created, it contains the name of the application that supplied the object, its data (or, in the case of a linked object, a reference to the data), and an image of the data.

Note   To place an object in an OLE container control, the component that provides the object must be registered in your system registry. When you install an application that supplies the objects you want to use in your project, that application should register its object library on your system so that application's objects appear in the Insert Object dialog box. You can use Regedit.exe to search the system registry for an object, but take care not to alter the contents of the registry.

Linked Objects

When you link an object, you are inserting a placeholder (not the actual data itself) for the linked object into your application. For example, when you link a range of spreadsheet cells to a Visual Basic application, the data associated with the cells is stored in another file; only a link to the data and an image of the data are stored in the OLE container control. While working with your Visual Basic application, a user can activate the linked object (by double-clicking the object, for example), and the spreadsheet application will start automatically. The user can then edit those spreadsheet cells using the spreadsheet application. When editing a linked object, the editing is done in a separate window outside the OLE container control.

When an object is linked to a Visual Basic application, the object's current data can be viewed from any other applications that contain links to that data. The data exists in only one place — the ActiveX component — which is the source application that provides the object. For example, in Figure 10.8, Visual Basic contains a link to the Graph application. Microsoft Word also contains a link to the graph. If the graph's data is changed by either application, the modified graph will appear in both the Visual Basic application and the Microsoft Word document.

Figure 10.8   An object's data can be accessed from many different applications that contain links to that data

As you can see, linking makes it easy to track identical information that appears in more than one application. Linking is useful when you want to maintain one set of data that is accessed from several applications.

Embedded Objects

To create an embedded object, you can either use an OLE container control or add an object's class to the Toolbox. With an embedded object, all the data associated with the object is copied to and contained in the OLE container control. When you save the contents of the control to a file, the file contains the name of the application that produced the object, the object's data, and a metafile image of the object. For this reason, embedded objects can greatly increase file size.

Unlike linked objects, no other application has access to the data in an embedded object. Embedding is useful when you want your application to maintain data that is produced and edited in another application, as shown in Figure 10.9.

Figure 10.9   Your application maintains data for an embedded object

When the user activates the object (the graph), the ActiveX component that created the object (Microsoft Graph) is invoked by the container application (your Visual Basic application), and the object's data is opened for editing. In addition, the user interface and menu system of the object is displayed in the container application so the user can control the object in place. For more information on in-place activation, see "Activating an Object in the OLE Container Control" later in this chapter.