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


Deactivating OLE Objects

You’ve inserted and activated a new OLE object in an OLE control. However, there doesn’t seem to be any way to deactivate the object now—the OLE server’s menus have taken over your program’s menu system, and there’s no Exit item. How do you close the OLE object?

Usually you click the form around an OLE object to deactivate that object. Deactivating an OLE object is easy: you just set its AppIsRunning property to False. You can also use the OLE control’s Close method to do the same thing.

As an example, we add this code to the oleinsert program we developed in the previous topic, allowing the user to deactivate OLE objects by clicking the form around the OLE control:

Private Sub Form_Click()
    OLE1.AppIsRunning = False
End Sub

Now the user can close an open OLE object just by clicking the form around the object.

Using Paste Special To Insert A Selected Part Of A Document Into An OLE Control

Besides using the InsertObjDlg method we saw two topics ago, you can also insert OLE objects using the Paste Special menu item in most OLE container programs. Besides embedding or linking to an entire document, Paste Special allows you to link or embed just the part of that document you want.

Users select the part of the document they want to embed or link to in the OLE server application, select the Copy item in that application’s Edit menu, and then select Paste Special in your container program to paste the part of the document they’ve selected into an OLE control. You support Paste Special with the OLE control’s PasteSpecialDlg method (this method takes no parameters).

Let’s see an example. Here, we add a Paste Special menu item to the Insert menu in the oleinsert example we developed two topics ago (see Listing 26.1). When users click that menu item, they’ve made a selection in an OLE server program, and we should paste that selection into our OLE control, OLE1. For this example, we select (using the mouse) a range of cells in an Excel spreadsheet, as shown in Figure 26.8, and copy them using Excel’s Copy menu item. (You must also have saved the Excel document you’re working with to disk so there is an actual document to which to link.)


Figure 26.8  Selecting and copying a range of cells in Microsoft Excel.

In the Paste Special event handler in our oleinsert example, then, we first check to make sure there’s something to paste with the OLE control’s PasteOK property; if True, we can paste an OLE object:

Private Sub PasteSpecial_Click()
    If OLE1.PasteOK Then
...

To get the object to paste, we use PasteSpecialDlg:

Private Sub PasteSpecial_Click()
    If OLE1.PasteOK Then
        OLE1.PasteSpecialDlg
    End If
...

The Paste Special dialog box appears at this point, indicating that we can paste an Excel object (see Figure 26.9). We can paste this object as an embedded object if we click the Paste option button, or as a link if we click the Paste Link option button. Here, we click Paste Link and click on OK. This pastes the new OLE link in our OLE control, OLE1, as shown in Figure 26.10.


Figure 26.9  The Paste Special dialog box.


Figure 26.10  Inserting a link to part of a document.

We can do one more thing here—we can check to make sure the OLE paste 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 PasteSpecial_Click()
    If OLE1.PasteOK Then
        OLE1.PasteSpecialDlg
    End If
    If OLE1.OLEType = vbOLENone Then
        MsgBox "OLE operation failed."
    End If

End Sub

That’s it—the new version of this program, oleinsert.frm version 2, appears in Listing 26.2. (The final version, Version 3—which is located in the insertole folder on this book’s accompanying CD-ROM—will let the user activate OLE objects from a menu.)

Listing 26.2 insertole.frm version 2

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
      Begin VB.Menu PasteSpecial
         Caption         =   "Paste special"
      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 Form_Click()
    OLE1.AppIsRunning = False
End Sub

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

Private Sub PasteSpecial_Click()
    If OLE1.PasteOK Then
        OLE1.PasteSpecialDlg
    End If
    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.