|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Deactivating OLE ObjectsYouve inserted and activated a new OLE object in an OLE control. However, there doesnt seem to be any way to deactivate the object nowthe OLE servers menus have taken over your programs menu system, and theres 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 controls 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 ControlBesides 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 applications Edit menu, and then select Paste Special in your container program to paste the part of the document theyve selected into an OLE control. You support Paste Special with the OLE controls PasteSpecialDlg method (this method takes no parameters). Lets 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, theyve 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 Excels Copy menu item. (You must also have saved the Excel document youre working with to disk so there is an actual document to which to link.)
In the Paste Special event handler in our oleinsert example, then, we first check to make sure theres something to paste with the OLE controls 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.
We can do one more thing herewe can check to make sure the OLE paste operation was completed successfully. To do that, well use the OLE controls OLEType property to check if theres an OLE object in OLE1. This property can take the values vbOLELinked, vbOLEEmbedded, or vbOLENone. Here, we check that property, and if its 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
Thats itthe new version of this program, oleinsert.frm version 2, appears in Listing 26.2. (The final version, Version 3which is located in the insertole folder on this books accompanying CD-ROMwill 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
|
|
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. |