The OLE Control

Visual Basic supports OLE linking and embedding via its OLE control. Using this control, you can add the functionality of complex programs, such as Word and Excel, directly into your programs with a minimum of effort. The best way to understand how the OLE control works is to see it in action, so the first thing you'll do is create a program that contains linked and embedded objects.

This example will embed a Microsoft Word document into a Visual Basic program. For the program to work, you must have Microsoft Word installed on your system. Although OLE enables you to "borrow" the functionality of other applications (servers) and use them in your own programs (containers), the server application must be present on whichever system you are running the container program.

note

If you don't have Microsoft Word, you can substitute any other kind
of linkable object available on your system.

To create the sample program, start a new project (Standard EXE). Use Table 26.1 as a guide for placing the controls on the form and building the program:

Table 26.1. The controls (and their property values) that make up the OLE test project

Control Type Property Value
Form Name frmOLETest
  Caption OLE Test
  Height 6030
  StartUpPosition 2 - Center Screen
  Width 7215
CommandButton Name cmdSave
  Caption Save
  Height 495
  Left 3000
  Top 4680
  Width 1215
OLE Name oleObject
  Height 4185
  Left 390
  Top 240
  Width 6435

Note that when you place the OLE control on the form, you will see the Insert Object dialog box shown in Figure 26.1.

Figure 26.1

The Insert Object dialog box appears automatically whenever an OLE control is added to a form.

The Insert Object dialog box provides a list of all object types available on your system (your list will most likely be different from the one shown in Figure 26.1). You are also given two options for how you want to insert the object: Create New or Create from File. If you select the Create New option, an object with no data (a new object) will be embedded into your application. If you select the Create from File option, a specific object will be inserted. For this example, select Create from File.

When you select the Create from File option, the list of embeddable objects disappears, and you are asked to specify a filename. You can use any file you want, but for this example, the Microsoft Word document OLETEST.DOC, which can be found on the CD that accompanies this book, will be used.

After you specify the file to be used, click the Open button. You will be returned to the Insert Object dialog box. For now, leave the Link and Display as Icon options unchecked. You'll learn more about them in just a moment. Click OK to insert the object.

You should then see a copy of the object (OLETEST.DOC or whatever file you specified) inside the OLE control (see Figure 26.2). You can now finish changing the OLE control's properties as shown in Table 26.1.

Figure 26.2

An embedded object (Microsoft Word file OLETEST.DOC) inside an OLE control.

Go ahead and run the program. The object still appears in the OLE control, no surprise there. But double-click on the OLE control and see what happens. You are allowed to edit the object (in this case, a Word document) right from within your application. Essentially, you've embedded a copy of the object, along with the server application that it belongs to, in your own application (see Figure 26.3).

Figure 26.3

Editing the embedded object from within the running VB application.

Notice also that after the document has been activated (by double-clicking on it), your application is provided with a menu bar that it did not have before. These menu options are taken directly from Word and can be used to make changes to the document, just like they would if you had loaded the document into Word and edited that way.

note

If your application already has a menu bar and an embedded object
is activated, it will merge its menu options with those of your
application.

You can make as many changes as you want to the embedded document, but you'll find that there is no way to save them because Word's File menu is missing. Why is that?

When you embed an object in Visual Basic, you have to use VB code to save changes to the object. This is accomplished using the OLE control's SaveToFile method.

Exit the program and then add the code in Listing 26.1 to the cmdSave CommandButton's Click event.

Private Sub cmdSave_Click()
Dim intFileNum As Integer
' Get the next available file number.
intFileNum = FreeFile
' Open a binary file with the name of the embedded
' object (oleObject.SourceDoc) and write out the
' object's data to that file using the OLE control's
' SaveToFile method.
If oleObject.OLEType = vbOLEEmbedded Then
   Open oleObject.SourceDoc For Binary As #intFileNum
   oleObject.SaveToFile intFileNum
   Close #intFileNum
End If
End Sub

In the preceding code, the SaveToFile method is only used if the object is embedded rather than linked (oleObject.OLEType = vb.OLEEmbedded). SaveToFile works with linked objects too (oleObject.OLEtype = vb.OLELinked), but it doesn't save the object's data. Instead, it saves the link information and an image of the data to the file.

To save the object's data to a file, you need to open a binary file, call the SaveToFile method, and then close the file. The filename used in the preceding procedure comes from the OLE control's SourceDoc property, which contains the name of the file specified in the Insert Object dialog box.

note

If you want to save the object in OLE version 1.0 format, use the
SaveToOle1File method in place of SaveOleFile.

If you run the program again, make some changes to the embedded object, and then click the Save button, the object's data will be saved. You can verify this by stopping the program and running it again. When the object data is loaded in, it should include the changes that you made.

To try some other variations of embedded objects, stop the program and then right-click the oleObject OLE control. You will see a pop-up menu of options. Choose Insert Object and the Insert Object dialog box opens.

Once again, choose the Create from File option and specify the OLETEST.DOC file (or any other object). Then make sure that the Link option is checked. Click OK, and you should see a message that asks you whether you want to delete the current embedded object (see Figure 26.4). Choose Yes.

Figure 26.4

After selecting a new object from the Insert Object dialog box, you are asked whether you want to delete the current embedded object.

You now have a linked object contained in your application. It might look the same as an embedded object, but as you'll soon see, it acts differently.

Run the program; then double-click the object. Instead of being able to edit the object within your application, the object's server application (in this case, Microsoft Word) is invoked. The object can then be edited and saved within the server application. When the file is saved, it is automatically updated in your application.

Stop the program; then go back to the Insert Object dialog box by right-clicking the oleObject control and choosing Insert Object from the pop-up menu. Choose the Create from File option; then specify the same file you've been working with. This time, leave the Link option unchecked, but check the Display As Icon option. Click OK to accept your choices.

You'll get another prompt, but this time it asks whether you want to delete the current link. Remember, a linked object is only a reference to an object and not the object itself. The last object that was inserted into the application was linked, not embedded. So you are asked whether you want to delete the link rather than the embedded object. Choose Yes.

Because you selected the Display As Icon option, you'll see an icon rather than the object's data (see Figure 26.5). If you run the program and double-click the icon, the object's server application will again be invoked, and the object can be edited (and saved). The Display As Icon option is useful if you don't want to actually display an object's data but still want the user to be able to edit it indirectly through your program.

Figure 26.5

An embedded Word document with the Display As Icon option selected.

If you don't want to embed an existing object, you can use the Create New option on the Insert Object dialog box. This will embed a blank object in the OLE container control. It's basically the same as using the Create from File option, but because it's a new file, there isn't any data associated with it. Figure 26.6 shows an embedded Microsoft Excel Chart object.

Figure 26.6

An embedded Microsoft Excel Chart object.

Along with the chart is a toolbar that enables you to specify how the chart will look. For example, you can choose the type of chart displayed by using the Chart Type pull-down icon. When the program is run, the toolbar will disappear. It reappears when the user double-clicks the embedded object to make it active.

There are probably dozens of different embeddable object types available on your system. You can use any of them in your programs, but keep in mind that the object's server application must be present on each user's system if you plan to distribute your programs. So if you choose to use Microsoft Excel charts in your application, you must be sure that anyone who uses the application also has Microsoft Excel installed on her system.

Top Home