Handling Changes to the OLE Container Control

The OLE Container control is used to dynamically add objects to a form at run time. For example, you can use an OLE Container to create a control that contains an embedded Microsoft Word document, create a control that contains a linked Word document, or bind to an OLE object in a database.

Visual Basic .NET does not support the OLE Container control. As a result, the upgrade wizard is unable to upgrade an OLE Container control. Instead, it replaces the control with a red label to highlight the need to rewrite the code.

Figure 9.4 illustrates a sample Visual Basic 6.0 application with an embedded OLE Container control holding an embedded Microsoft Word document.

Figure 9.5 illustrates the Visual Basic .NET version after applying the upgrade wizard.

The code produced by the upgrade wizard is marked with UPGRADE_ISSUE comments wherever OLE Container controls appear. The following is an example of such a comment.

' UPGRADE_ISSUE: Ole method OLE1.CreateLink was not upgraded.

Vb6 Containers

Figure 9.4

OLE Container control with linked object in Visual Basic 6.0

Figure 9.4

OLE Container control with linked object in Visual Basic 6.0

Figure 9.5

OLE Container control after applying the upgrade wizard

Figure 9.5

OLE Container control after applying the upgrade wizard

A strategy for replacing the OLE Container is to use a WebBrowser ActiveX control in place of the OLE Container to display the Word document in the form. Through this control, you can display, edit, and save documents displayed in the control.

Figure 9.6 illustrates an updated version of the Visual Basic .NET code that has replaced the OLE Container with a WebBrowser ActiveX control to display the Word document.

Figure 9.6

A Microsoft Word document displayed using the WebBrowser control

Figure 9.6

A Microsoft Word document displayed using the WebBrowser control

► To replace the OLE Container with a WebBrowser ActiveX control

1. Remove the red label inserted by the upgrade wizard as a placeholder for the OLE Container.

2. Add a WebBrowser control to the form. You can do this by right-clicking in the ToolBox, clicking Add/Remove Items, clicking the COM Components tab, and then clicking Microsoft WebBrowser Control in the Customize Toolbox component picker.

3. After you add the control, add the following code to the Form_Load subroutine (or wherever you want to bind to the document):

Me.AxWebBrowser1.Navigate("C:\Temp\LinkedDoc.doc")

Be sure to substitute the file name in this example for the location of the object to which you are linking. After completing these steps, you should be able to rebuild the solution.

Note that this method works for most objects that can be displayed in Microsoft Internet Explorer, such as HTML files, Word documents, spreadsheets, GIFs, and JPEG files.

Was this article helpful?

0 0

Post a comment