|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Copying And Pasting OLE Objects With The ClipboardYou can copy an OLE object to the Clipboard with an OLE controls Copy method, and you can paste an OLE object in the Clipboard to an OLE control with that controls Paste method. Neither of these methods takes any parameters. Lets see an example. Here, we can copy the OLE object in the OLE control OLE1 when the user clicks a command button, Command1, and paste that object into another OLE control, OLE2, when the user clicks a second command button, Command2. When the user clicks Command1, then, we just copy the OLE object in OLE1 to the Clipboard (note that an OLE object should be running when you copy it to the Clipboard):
Private Sub Command1_Click()
OLE1.Copy
End Sub
When the user clicks Command2, we should paste the OLE object from the Clipboard to OLE2. First, we check to make sure there is an object in the Clipboard that the OLE2 control will accept, using that controls PasteOK method:
Private Sub Command2_Click()
If OLE2.PasteOK Then
...
End If
If its okay to paste into OLE2, we use that controls Paste method to paste the OLE object in the Clipboard into OLE2:
Private Sub Command2_Click()
If OLE2.PasteOK Then
OLE2.Paste
End If
...
Finally, we check to make sure the paste operation was completed successfully:
End SubPrivate Sub Command2_Click()
If OLE2.PasteOK Then
OLE2.Paste
End If
If OLE2.OLEType = None Then
MsgBox "OLE operation failed."
End If
End Sub
Zooming OLE ObjectsThe Testing Department is on the phone again. The OLE control youre using to show a picture of the companys glorious founder is great, but cant it be larger? You can zoom (that is, enlarge or shrink) OLE objects using the SizeMode property of OLE controls. Here are the possible values for the SizeMode property:
Lets see an example. Add a new OLE control to a form, setting its SizeMode property to VbOLESizeZoom. Next, we add a button, Command1, with the caption Zoom and add this code to enlarge OLE1 when the user clicks that button:
Private Sub Command1_Click()
OLE1.Width = 3 * OLE1.Width
OLE1.Height = 3 * OLE1.Height
End Sub
Thats all it takes. As an example, were zooming the Excel spreadsheet you see in Figure 26.15.
Saving And Retrieving Embedded Objects DataBecause an embedded OLE objects data is stored in your program, you are responsible for storing and retrieving that data if its changed. Otherwise, when a form containing an OLE container control is closed, any changes to the data associated with that control are lost. To save updated data from an object to a file, you use the OLE container controls SaveToFile method. Once the data has been saved to a file, you can open the file and restore the object with the ReadFromFile method. Lets see an example. When the user clicks a button, Command1, we can open a new file, data.ole, for binary output:
Private Sub Command1_Click ()
Dim intFileNumber as Integer
intFileNumber = FreeFile
Open "data.ole" For Binary As #intFileNumber
...
Now we can save the OLE object in an OLE control, OLE1, to that file this way using SaveToFile:
Private Sub Command1_Click ()
Dim intFileNumber as Integer
intFileNumber = FreeFile
Open "data.ole" For Binary As #intFileNumber
OLE1.SaveToFile intFileNumber
Close #intFileNumber
End Sub
Later, we can read the stored OLE object back to OLE1 with the ReadFromFile method when the user clicks another button, Command2. First, we open the data.ole file:
Private Sub Command2_Click ()
Dim intFileNumber as Integer
intFileNumber = FreeFile
Open "data.ole" For Binary As #intFileNumber
...
Then we read the OLE object back into OLE1 with the ReadFromFile method:
Private Sub Command2_Click ()
Dim intFileNumber as Integer
intFileNumber = FreeFile
Open "data.ole" For Binary As #intFileNumber
OLE1.ReadFromFile intFileNumber
Close #intFileNumber
End Sub
And thats itnow weve stored and retrieved an OLE file to and from disk (which is called persisting the object). Handling OLE Object Updated EventsYou can determine when an OLE object has been updated with the OLE controls Updated event. Your program is passed a variable named code in the Updated event handler to indicate what operation updated the OLE objects data: Sub OLEobject_Updated (code As Integer) Here are the possible settings for code:
|
|
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. |