![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Saving OLE Objects To Disk The data associated with each OLE object is saved in its own file. This is a standard Basic file, opened in binary mode using the file commands that will be covered in Chapter 13. Once the file is open, call the OLE controls SaveToFile method, passing the number of the open file as an argument. For example, for an OLE control named Ole1, the following code will save its data in a file named MYOLEFILE.DAT: FileNum = FreeFile Open MyOleFile.Dat For Binary as #FileNum Ole1.SaveToFile FileNum Close #FileNum The procedure for saving an OLE object is the same for both linked and embedded OLE objects; but as mentioned previously, the nature of the saved data differs. For a linked OLE object, an image or picture of the data is saved to the file, along with the link information identifying the source data file. The objects actual data, youll remember, is maintained by the server application in a file of the applications usual format. For an embedded OLE object, the actual data is saved in the Basic file. Be aware that the OLE control does not keep track of whether or not its data is saved. If you load a new object into the control, any existing data will be overwritten without warning. It is the responsibility of the program to keep track of an OLE objects data statuseither saving it automatically or prompting the user to save when necessary. Retrieving OLE Objects From DiskRetrieving an OLE object from disk is essentially the reverse of the procedure used to save it. Open the data file in binary mode, then call the OLE controls ReadFromFile method, passing the file number as an argument. Heres an example: FileNum = FreeFile Open MyOleFile.Dat For Binary as #FileNum Ole1.ReadFromFile FileNum Close #FileNum If you are unsure about using these file commands, I suggest that you read Chapter 13. Other OLE Control Properties, Methods, And EventsFor an OLE container that contains a linked object, the possibility always exists that the data in the linked file has been modified by another program (it could be the server application or another OLE container application that is linked to the file). To be sure that the OLE control displays the current version of the data, execute the OLE controls Update method. This may not be necessary, however, depending on the OLE controls UpdateOptions property setting. This property has three possible settings:
If the setting is Automatic, the Update method will never be necessary. Of course, the UpdateOptions property applies only to linked OLE objects. Because embedded objects store their own data, they never need updating. You can use the OLE controls Updated event to detect when an OLE object has been updated by the server application. This event is triggered each time the server application updates an OLE object. The syntax for this event procedure is: Sub OLE_Updated (Code As Integer) The Code argument indicates how the OLE object was updated. Its possible values are as follows (the constant names in parentheses are predefined Windows constants that you can use in your code):
This event procedure is frequently used to inform the program that the data in a linked OLE control has been changed since it was last saved. Code in the Updated event procedure can set a global data changed flag. The program can test this flagfor example, on exitto determine if the data needs to be saved. You must declare and update this flag yourself, in code. Activating OLE ObjectsActivating an OLE object means starting its server application to manipulate the object. For an OLE control, the default method of activation is to double-click on it or to move the focus to the OLE control and press Enter. Other activation methods are possible, depending on the details of the server application and on the setting of the OLE controls AutoActivate property. Table 10.1 shows the possible settings of this property.
Putting OLE To WorkThis book is called the Visual Basic Programming Blue Book, but so far, all youve been doing in this chapter is sitting around looking at maps. Unavoidablebut hardly exciting. I could present plenty more OLE details, but youve already learned the important stuffmore than enough to put OLE to work in a real-world Visual Basic application. So lets get to work and take OLE out for a spin. After all, the best way to learn is by rolling up your sleeves and trying it out. In this section, Ill develop a basic OLE container application that demonstrates how some of the techniques presented earlier are applied in a real program. Youll see how to create a new embedded object, how to edit an existing object, and how to save and retrieve objects on disk. OLE_DEMO.VBP consists of a single form containing only an OLE control and a menu. Other than changing the forms Caption property to OLE Demonstration, you can leave all object properties at their default values. The two menus consist of a File menu with Save and Exit commands and an Object menu with Insert and Delete commands. Listing 10.1 presents the forms objects and properties.
|
![]() |
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. Read EarthWeb's privacy statement. |