Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


For example, we’ve set the DisplayType property of the OLE control in Figure 26.6 to vbOLEDisplayIcon so it displays the icon of its OLE object, which in this case is an Excel object. Note that you cannot open an iconic OLE object for in-place editing, but you can open it for editing in the OLE server program (in other words, when you double-click the object, the OLE server application opens in its own window).


Figure 26.6  Displaying an OLE object in iconic form.


TIP:  Note that you must set the DisplayType property before you insert an OLE object into the OLE control because you cannot set the DisplayType property while the OLE control contains an object.

Using The OLE Control’s Pop-Up Menus At Design Time

When you add an OLE control to a form, the Insert Object dialog box appears, and you can insert an object into the control at that time. However, if you don’t want to insert an object right away, you can use the control’s pop-up menu later. To open the OLE control’s pop-up menu, just right-click the control at design time. Here are the primary items in the menu that appears:

  Insert Object—Open the Insert Object dialog box.
  Paste Special—Link to Clipboard object.
  Delete—Delete embedded object.
  Create Link—Create a link to the document in the SourceDoc property.
  Create Embedded Object—Creates an embedded object using the Class or SourceDoc property.

Note that you can link to or embed a document in the OLE control if you’ve set the control’s SourceDoc property. You can also create a new embedded object if you’ve set the control’s Class property; classes specify what code components (formerly called OLE automation servers) are available. (We’ll see how classes work in the next chapter.)


TIP:  You can get a list of the class names available to your application by selecting the Class property in the Properties window and clicking the arrow button in that property’s entry.

Inserting An OLE Object Into An OLE Control At Runtime

The Testing Department is on the phone again. It’s fine that you’ve been able to embed an OLE object in an OLE control at design time, but don’t you think it would be great if you could let users embed their own OLE objects? Maybe, you say.

To let users insert their own OLE objects into an OLE control, you can display the same Insert Object dialog box that appears at design time when you created the OLE control, and you use the control’s InsertObjDlg method to do that (this method takes no parameters).

Let’s see an example. Add an OLE control, OLE1, to a form now, and click Cancel when the Insert Object dialog box appears. Set the OLE control’s SizeMode property to VbOLESizeAutoSize (note that when you set an OLE control’s SizeMode property to VbOLESizeAutoSize, the OLE server sets the size of the OLE object, so the size of the OLE control, OLE1, will probably change when the object is inserted). Finally, add an Insert menu to the form with one item in it: Insert Object.

When users click the Insert Object menu item, they want to insert an OLE object in OLE1, and we can let them create and embed a new object, embed an existing file, or embed a link to an existing file with the InsertObjDlg method. We do so like this:

Private Sub InsertObject_Click()
    OLE1.InsertObjDlg
...
End Sub

When we execute this method, InsertObjDlg, the program displays the Insert Object dialog box, which appears in Figure 26.4. Using this dialog box, the user can embed new objects, existing files, or link to existing files. In fact, that’s all the code we need—when the InsertObjDlg method finishes, it inserts the new OLE object in the OLE control.

We can do one more thing here—we can check to make sure the OLE insertion operation was completed successfully. To do that, we’ll use the OLE control’s OLEType property to check if there’s an OLE object in OLE1. This property can take the values vbOLELinked, vbOLEEmbedded, or vbOLENone. Here, we check that property, and if it’s set to vbOLENone, we indicate to the user that there was an error:

Private Sub InsertObject_Click()
    OLE1.InsertObjDlg
    If OLE1.OLEType = vbOLENone Then
        MsgBox "OLE operation failed."
    End If
End Sub

Using this code allows us to insert OLE objects like the Excel spreadsheet you see in our OLE control in Figure 26.7. Here, when the object is first inserted, it’s opened for in-place editing by default.


Figure 26.7  Inserting an OLE object into a program.


TIP:  The Insert Object dialog box only lets the user perform the actions you’ve allowed with the OLETypeAllowed property; this property can be set to Linked (0), Embedded (1), or Either (2).

There’s a problem here, however. Now that we’ve inserted a new OLE object and activated it, how do we deactivate it? There are no Exit items in the Excel menu system that’s taken over our program’s menu system. Usually you deactivate an OLE object by clicking the form around the OLE control, and we’ll see how that works in the next topic.

The code for this example so far, insertole.frm version 1, appears in Listing 26.1 (version 2 will also support a Paste Special menu item). (The final version of this code, version 3, is located in the insertole folder on this book’s accompanying CD-ROM.)

Listing 26.1 insertole.frm version 1

VERSION 6.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   2115
   ClientLeft      =   165
   ClientTop       =   735
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   2115
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.OLE OLE1
      Height          =   1095
      Left            =   840
      SizeMode        =   2  'AutoSize
      TabIndex        =   0
      Top             =   360
      Width           =   3015
   End
   Begin VB.Menu File
      Caption         =   "File"
   End
   Begin VB.Menu Insert
      Caption         =   "Insert"
      Begin VB.Menu InsertObject
         Caption         =   "Insert object"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub InsertObject_Click()
    OLE1.InsertObjDlg
    If OLE1.OLEType = vbOLENone Then
        MsgBox "OLE operation failed."
    End If
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.