The OLE Container Control VB Help

Now, we’ll look at how you can exploit’the OLE technology from within your Visual Basic applications. To incorporate OLE functionality into your VB applications, you need the OLE Container control, which allows you to insert objects from other applications into your program. You can place only one object in the Container control at a time, but you can have multiple OLE Container controls on the same Form, each one with its own object.

The OLE Container control is your door to the various objects available within the operating system that Visual Basic can’t handle on its own, including Word documents, sounds, bitmaps, and Excel spreadsheets. The OLE Container control can host any of the objects listed in the Insert Object dialog box, and in essence, make your Visual Basic application act like a container application. The good news is that the OLE Container control hides nearly all the complexity of OLE and you can easily build OLE-capable applications. You can embed objects in OLE Container control at design time or at runtime.

Object Linking and Embedding at Design Time

In this section, we are going to create a simple OLE,Container control to demonstrate object embedding at design time. Embed objects at design time if you know in advance the object to be embedded and you want to limit the users of the application to a specific object or type of object (a Word document, for example). It’s
also possible to link objects with the following techniques, but at design time, you usually embed, rather than link, documents. You can embed objects in two ways at design time:

• By pasting existing object (or dragging-and-dropping the object on the control).

• By selecting the object through the Insert Object dialog box.

Embedding with Drag and Drop

To embed with drag-and-drop, follow these steps:

1. Start a new Visual Basic project and double-click the OLE Container control’s icon in the Toolbox to place an instance of’the control on the Form, Visual Basic will display the Insert Object dialog box, where you can specify the object to be embedded in the OLE Container control.
2. In the Insert Object dialog box, check the Cancel button. (You want an empty OLE Container control or that you can later drop’ an OLE object on it.)
3. In the OLE Container control’s Properties window, change the Size Mode property to l-Stretch (if it’s a drawing that can be resized)or to 2- Auto Size (if it’s text or a bitmap that will look bad when resized) to adjust the size of the object or the container.

The settings of the Size Mode property are explained in Table:

TABLE 14.1

4. Next, start an application that supports OLE and open a file. In this example, we will embed a Word document in our OLE Container control by dragging and dropping the document from the source application (Word) onto the OLE Container control.
5. With both Visual Basic and Word visible on your screen, drag the document (or part of it) onto the empty OLE Container control.
6. As you drag the object, an oblong shape appears under the pointer arrow. Drop this onto the empty OLE Container control. After a few seconds, the , Word document will appear in your OLE Container control.

FIGURE 14.5

Your Form now contains a Word document. When you run the application, the Word document will be displayed in the OLE Container control. You can edit it with Word’s menus and tool bars.

Class, SourceDoc, and Source Item Properties

Let’s look at a few more basic properties of the OLE Container control. When you create an object on your Form, the Properties window contains the following information:

• The name of the application that produced the object (property Class)
• The data or a reference to the data (property SourceDoc)
• The image of the data (property SourceItem). This applies to linked objects only. It specifies the data within the source file that are linked to the OLE control.

OLE Type Allowed Property. Whether the source document is embedded in or linked to the OLE Container control depends on the setting of the control’s OLE Type Allowed property. Select the OLE Container control on the Form and locate the OLE Type Allowed property in the Properties window.

Capture

Let’s make a linked object by setting OLE Type Allowed property to (Linked). The default setting of this property doesn’t determine what kind of OLE type you’re going to use, but rather what type can be used. The actual OLE type is indicated in the Insert Object dialog box. For example, if OLE Type Allowed is 2, the Create New and Create from File buttons and the Link Check Box control are enabled in the Insert Object dialog box. If the OLE Type Allowed property is 0 (Linked), the Create New button is disabled (you can’t link a nonexistent object). If you select 1 (Embedded), the Link check box is disabled.

Linking with the Insert Object Dialog Box

Dragging-and-dropping is not the only way to embed or link objects in an OLE Container control. You can also do so through the Insert Object dialog box by following these steps:

1.. Delete the OLE Container control in Form1 and create a new OLE Container control as you did earlier.
2. In the Insert Object dialog box, check the Create- from File option and the Link check box, as shown. As mentioned, the Link check box is sometimes disabled.. This occurs when you’ve set OLE Type Allowed to I-Embedded. To make the LW <checkbox available, set the OLE Type Allowed to 2-Either .
3. Click the Browse button to locate the file you want.
4. The selected file will appear on the OLE Container control. You’ll see part of the linked file. To fit the entire object in the container, resize the control.

Depending on the setting of the Size Mode property, the image of the linked file may be distorted. The OLE Container control now contains a reference to the object, not a copy of the object that embedded objects use. Whenever the server application changes the linked object, the object in your container will be updated.

FIGURE 14.6

If the linked object is a Word document it will appear in your OLE Container control as shown. You’ll notice that the Source Doc property is now the path file name of the linked object.

5. Keep the Visual Basic window open while you open the document in Word. Make some changes to the document and watch the linked document in the Visual Basic window get updated real time. You don’t have to run the application for this to happen. Even in design mode, the linked document is updated in real time as the original is edited.

Saving and Retrieving Embedded Objects

Embedded objects are created by server applications, but are stored in your container application. Once an object is embedded, there is no connection between the contents of the OLE Container control and the original object. To edit the embedded object, you access the resources of the application that supplied the object, but the data is supplied by the container application. Moreover, data in an embedded object is not automatically saved. To save the embedded object and any changes made by the server application, you use the SaveToFile method. It has the following syntax:

OLE1.SaveToFile filenumber

The file number variable is a numeric expression specifying an open file’s number. The number must correspond to a file opened in binary mode. The following code demonstrates how to prompt the user for a file name and save the embedded document in it with the SaveToFile method.

Capture

To load an object that has been saved to a data file, use the ReadFromFile method. Its syntax is similar to that of SaveToFile. The following code demonstrates how to read the contents of an OLE container from a file (where it was stored earlier with the SaveToFile method) .

Capture

When you save an embedded file, the following information is saved:

• The name of the application that created the object
• The object’s data
• A meta file image of the object
When you save a linked file, the following information is saved:
• The name of the application that created the object
• The file name of the object
• A meta file image of the object.

The data saved by the SaveToFile method is only accessible by the container application and only through the ReadFromFile method. These are Visual Basic methods that you can invoke from within your applications without knowing anything about the object’s native format.

Using In-Place Editing

If you run the application now, you’ll see the embedded or linked object right on your Visual Basic Form. The user of the application can actually edit the object by double-clicking it. You can open the embedded object for editing in several ways, which are discussed in the sections “Common OLE Container Control Properties” and “Common OLE Container Control methods,” later in this chapter. The simplest way to open the application that provides the embedded object and edit the object using its menus and tool bars is to double-click the object.

In-place editing, or in-place activation, is an enhancement to embedded objects in Visual Basic. With this feature, you can edit the embedded object within the OLE container. The functionality of the server application is incorporated into the container. In-place editing is available only if the server application supports OLE Automation. When you select the embedded object, the menus and toolbars of the server application replace the menus and toolbars of the Visual Basic application. In-place editing allows your container application to borrow the functionality of the embedded object’s server application.

FIGURE 14.7:

Posted on November 6, 2015 in OLE Automation and VBA

Share the Story

Back to Top