|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
For example, weve 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).
Using The OLE Controls Pop-Up Menus At Design TimeWhen 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 dont want to insert an object right away, you can use the controls pop-up menu later. To open the OLE controls pop-up menu, just right-click the control at design time. Here are the primary items in the menu that appears:
Note that you can link to or embed a document in the OLE control if youve set the controls SourceDoc property. You can also create a new embedded object if youve set the controls Class property; classes specify what code components (formerly called OLE automation servers) are available. (Well see how classes work in the next chapter.)
Inserting An OLE Object Into An OLE Control At RuntimeThe Testing Department is on the phone again. Its fine that youve been able to embed an OLE object in an OLE control at design time, but dont 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 controls InsertObjDlg method to do that (this method takes no parameters). Lets 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 controls SizeMode property to VbOLESizeAutoSize (note that when you set an OLE controls 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, thats all the code we needwhen the InsertObjDlg method finishes, it inserts the new OLE object in the OLE control. We can do one more thing herewe can check to make sure the OLE insertion 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 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, its opened for in-place editing by default.
Theres a problem here, however. Now that weve inserted a new OLE object and activated it, how do we deactivate it? There are no Exit items in the Excel menu system thats taken over our programs menu system. Usually you deactivate an OLE object by clicking the form around the OLE control, and well 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 books 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
|
|
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. |