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


Copying And Pasting OLE Objects With The Clipboard

You can copy an OLE object to the Clipboard with an OLE control’s Copy method, and you can paste an OLE object in the Clipboard to an OLE control with that control’s Paste method. Neither of these methods takes any parameters. Let’s 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 control’s PasteOK method:

Private Sub Command2_Click()
        If OLE2.PasteOK Then
...
        End If

If it’s okay to paste into OLE2, we use that control’s 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 Objects

The Testing Department is on the phone again. The OLE control you’re using to show a picture of the company’s glorious founder is great, but can’t 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:

  vbOLESizeClip—0 (the default); Clip. The object is displayed in actual size. If the object is larger than the OLE control, its image is clipped by the control’s borders.
  vbOLESizeStretch—1; Stretch. The object’s image is sized to fill the OLE control. (Note that the image may not maintain the original proportions of the object.)
  vbOLESizeAutoSize—2; Autosize. The OLE control is resized to display the entire object.
  vbOLESizeZoom—3; Zoom. The object is resized to fill the OLE container control as much as possible while still maintaining the original proportions of the object.

Let’s 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

That’s all it takes. As an example, we’re zooming the Excel spreadsheet you see in Figure 26.15.


Figure 26.15  Zooming an OLE object.

Saving And Retrieving Embedded Object’s Data

Because an embedded OLE object’s data is stored in your program, you are responsible for storing and retrieving that data if it’s 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 control’s SaveToFile method. Once the data has been saved to a file, you can open the file and restore the object with the ReadFromFile method. Let’s 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


WARNING!  If you save multiple OLE objects to a file, you should read them from the file in the same order you wrote them.

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 that’s it—now we’ve stored and retrieved an OLE file to and from disk (which is called persisting the object).

Handling OLE Object Updated Events

You can determine when an OLE object has been updated with the OLE control’s Updated event. Your program is passed a variable named code in the Updated event handler to indicate what operation updated the OLE object’s data:

Sub OLEobject_Updated (code As Integer)

Here are the possible settings for code:

  vbOLEChanged—0; the object’s data was changed.
  vbOLESaved—1; the object’s data was saved by the application that created the object.
  vbOLEClosed—2; the file containing the linked object’s data was closed by the server application that created the object.
  vbOLERenamed—3; the file containing the linked object’s data was renamed by the server application that created the object.


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.