The OLE Control’s Shortcut Menu VB Help

Another way to embed objects in an OLE Container control is through the control’s shortcut menu. Right-click an OLE Container control at design time to display the shortcut menu shown. Use this menu to insert a new object or to edit the embedded document if the control already contains an object. To embed (or link) another object in an OLE Container control, first delete the existing object.

The commands on the shortcut menu depend on the state of the selected object. The shortcut menu contains the Edit and Open commands since the selected object is a Word document. If the embedded object is a sound file, the shortcut menu will contain the Edit and Play commands.If the control doesn’t contain an object (because you clicked the Cancel button in the Insert Object dialog box), the Edit and Open commands, which depend on the control’s contents, will be missing from the shortcut menu.

The shortcut menu of the OLE Container may contain some or all of the following commands:

Cut Copies the object in the container to the Clipboard and clears the container .
Copy Copies the object in the container to the Clipboard
Paste Pastes an object from the Clipboard to the control
Delete Removes the OLE object from the OLE control
Insert Object Deletes the existing object and opens the Insert Object dialog box so that the user can insert a new or an existing object in the OLE Container control
Paste Special Opens the Paste Special dialog box, which allows you to paste an object copied to the Clipboard from another OLE application. Whereas the Paste command embeds the object in the control, the Paste Special command creates a link to the object on the Clipboard. The Paste Special dialog box is similar to the Insert Object dialog box, except that its options apply to the object currently stored on the Clipboard.

Create Link Appears only if the SourceDoc property of the OLE Container control is set; creates a linked object
Delete Link , Breaks a link and turns a linked object into an embedded object.
• Create Embedded Object Appears if either the SourceDoc or the Class properties are set; creates an embedded object.

Object Linking and Embedding at Runtime

In this section, we are going to create linked and embedded objects at runtime, This section’s example is called OLER Time, and you’ll find it in this chapter’s folder on the CD. The OLER Time application demonstrates how to:

• Insert an object
• Choose between linking and embedding
• Set the size of the container control or the size of the object
• Display information about the object.

The OLER Time application demonstrates how you can embed objects in and link them to an OLE Container control by opening the Insert Object dialog box from within your code. It programmatically does what you’ve been doing manually in the previous examples. Operations are performed from within the framework of a Visual Basic application, which lets the user select the object and set some of its basic properties. The application’s main Form is shown.

When you click the Insert Object button, the application displays the Object Type dialog box in which you set the container’s Stretch mode and OLE type (link or embed). If you click OK, you’ll display the Insert Object dialog box, in which you can specify a new object or select an existing one.

To test the application, check the Stretch Container Control and Embedded options in the Object Type dialog box, and click OK. In the Insert Dialog box that appears, select the Excel Chart object. A chart with random data will appear on the control. You can edit the chart from within the OLER Time application, resize the graph (because the Stretch Container Control option is checked), and even save the revised chart to a local file. You can stop the program, run it again and load the chart saved in the local file.

VB6 at Work: The OLER Time Application

To design the application, follow these steps:

1. Start a new Visual Basic project, and change the Form’s name to frmOLE. This Form contains an OLE Container control, five Command buttons  and the Windows Common Dialogs control.
2. When you place the OLE Container control on the Form, the Insert Object dialog box will appear. Click Cancel to place an empty container on the Form. Resize the OLE Container control, and place on it the Command buttons shown.
3. Add a new Form to the project and place the controls you see  on it.
4. Save all the Forms and the project in a new folder .

Now, you’ll add the code that will do the following:

• Display the second window (frmType) and activate the Insert Object dialog box
• Display information about the object.

Open the Insert Object button’s Click handler and enter the following:

The Object Info button displays information about the object that has been inserted in the container (you’ll see shortly how the object is selected and inserted). The following code is executed when you click the Object Info button (ole Display is the name of the OLE control).

The carriage return character (vbCiLF) breaks the message into multiple short lines. If the embedded (or linked) object is a Word document, the following information is displayed:

(FileName will be replaced by the name of the.file you actually embed in the OLE control.)It shows what the Object Info button displays if an Excel Chart object is embedded to the OLE Container control. The Sourceltem is blank for a new Excel Chart object.

SourceDoc and Class Properties

The Object Info button displays the values of the SourceDoc and Class properties. The SourceDoc property is the name of the linked or embedded file. The Class property sets or returns the class name of an embedded object, which is its type. Other object types you’ll find on your computer are Excel spreadsheets (Excel.Sheet), Paint pictures, and objects you’ll create in the remaining sections of this chapter. If you create a new object using Excel, Word, or another compatible OLE application, there will be no SourceDoc property because the embedded object is not yet a document that resides on the disk.

The Source Item Property

The SourceItem property indicates the part of the document that is embedded or linked. In many cases, this property is undefined because the entire object is linked. You can, however, select PCN of some objects for embedding: You can’t select part of a Word document or a bitmap, but you can select part of’an Excel spreadsheet. If you select a range of cells, the Source- Item property might be something like “RICL : RICLO.” Although the OLER Time application allows you to select the file to be embedded, it doesn’t support drag and drop, so the Sourceltem property is empty.

When you click the Insert Object button, the application displays a window that offers you a choice of data types. You can select Linked or Embedded. You can also determine the size of-the object to be displayed by setting the size mode in the OLE Container control’s Properties window, The choices are:

• Stretch the OLE Container control to fit the object (2-AutoSize)
• Stretch the object to fit the container (I-Stretch).

When you click OK, the Insert Object dialog box opens, and you can select the data to insert.

Draw the Command buttons, Option buttons, and frames as shown previously Then add the following code for Cancel button.

This Sub procedure close;; the application by unloading frmType.

Setting the Size of the OLEContainer Control

Let’s digress for a moment and set the size for the OLE Container control. When embedding objects in an OLE Container control at runtime, you must take into consideration the size of the control because the user can’t change it with the mouse. Let’s declare the variables OLE Height and OLE Width so that the size of the control will be the size you select at design time. Instead of hard coding the height and width of the container control, we’ll use OLE Height and OLE Width, which are more flexible. In the frmOLE Form’s declaration section enter the following:

In the Form’s Load event add the following lines:

Now return to frmType and add the code to call the Insert Object dialog box after the user sets the desired options.

Add the following line to hide the Form:


Finally, the following lines open the Insert Object dialog box and insert the object:

The Object Info button becomes enabled only if an object is inserted.

InsertObjDlg is a method of the OLE Container control that displays the Insert Object dialog box so that the user can create a linked or an embedded object by choosing its type and the application that provides the object. The user’s choices are . automatically assigned to the appropriate properties of the OLE control.

To size the container, follow these steps:

1. Run the application and, click the Insert Object button to display the Object Type dialog box.
2. Click one of the Size Mode buttons, and then click one of the.object Type buttons. The Insert Object dialog box appears as shown.

Here’s the complete listing of the frmOLE Form. Notice the Resize event handler of the OLE Container control. If the control is allowed to be resized at runtime, the Resize event is triggered every time the user resizes it. This event reports the new dimensions of the control with the HeightNew and WidthNew arguments. These values_are used by the handler to resize the Form accordingly.

Try embedding various objects in the OLE control of the OLER Time Form and double-dick the control to edit them, You’ll see that the OLE Container control is still somewhat rough around the edges, It takes a while to embed or link a document, and sometimes when you switch to edit mode, the server application’s toolbars may not be visible: Embedding objects in the OLE Container control is not used commonly in Visual Basic programming. Later in this chapter, you’ll learn how to OLE automate server applications and how to instruct the server application to carry out some tasks . and return the results to the Visual Basic application.

Posted on November 6, 2015 in OLE Automation and VBA

Share the Story

Back to Top
Share This