![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
To have the object display an icon, rather than the data, in your application, select the Display As Icon option in the Insert Object dialog box. Youll still be able to activate the object for editing by double-clicking on the icon, but you wont be able to view the data in the Visual Basic application. If you dont need to view the data, this choice can speed up screen display. If you dont want to insert an object now, click on the Cancel button in the Insert Object dialog box, and Visual Basic will place an empty OLE control on your form. You can still insert an object later during program design. To do so, right-click on the OLE control and select Insert Object from the menu that is displayed. The Insert Object dialog box will be displayed, and you can proceed as described earlier.
Using Paste Special During Program DesignAnother option during program design is to place an object in an OLE control using the Paste Special command. This method is necessary when the object you want to link does not correspond to an entire data file. For example, you can create an object from a single paragraph of a word-processing document or a block of cells in a spreadsheet. Start by placing the OLE control on your Visual Basic form and leaving it empty by selecting Cancel when the Insert Object dialog box is displayed. Then follow these steps:
Inserting An Object At RuntimeIf you dont want the OLE objects in your Visual Basic application to be fixed, they must be inserted while the program is executing. The OLE control is left empty during program design; when the program runs, either the program code or the user will place the desired data object in it.
The CreateEmbed And CreateLink MethodsTo create an embedded object, use the OLE controls CreateEmbed method. This method allows you to create a new embedded object or one that is based on an existing file. The syntax of CreateEmbed is as follows: object.CreateEmbed sourcedoc [class] The identifier object specifies the name of the OLE control. If you want to embed an object based on an existing file, sourcedoc specifies the file, and class can be omitted (or, if not omitted, it is ignored). If you want to create a blank object of a certain class, the class argument specifies the class of the object, and the sourcedoc argument must be a blank string. Heres an example: If you have an OLE control named OLE1, the command OLE1.CreateEmbed c:\data\report1996.xls creates an embedded object based on the spreadsheet file REPORT1996.XLS in the folder C:\DATA. In contrast, the command OLE1.CreateEmbed , Excel.Sheet.6 creates a new, blank object based on an Excel version 6 workbook. You can use the CreateEmbed method only if the OLE controls OLETypeAllowed property is set to Embedded or Both. Executing this method will erase any object currently in the OLE container without warning. As youll soon see, it is your programs responsibility to save the existing object. The CreateLink method is used to create a linked object. The syntax is as follows: object.CreateLink sourcedoc [,sourceitem] The required argument sourcedoc specifies the data file to which the object is to be linked. The optional argument sourceitem identifies the link data item within the file. Lets look at a couple of examples. The statement OLE1.CreateLink Evaluation.Doc creates a link to the entire Microsoft Word document Evaluation.Doc. The command OLE1.CreateLink Evaluation.Doc, Bookmark6 creates a link to the text in the same document that is identified by the bookmark named Bookmark6. The CreateLink method can be used only if the OLE controls OLETypeAllowed property is set to Linked or Both. Successful use of both of these methods requires that the specified data file is present and (when required) that the specified link item is present in the file. If not, an error occurs. Also, the class specified for the CreateEmbed method must be a valid class that is present in the system registry. Using A Dialog Box To Insert An Object At RuntimeI explained earlier in the chapter how to use the Insert Object dialog box during program design to insert an object in an OLE control. You can display the same dialog box during program execution by executing the OLE objects InsertObjDlg method. This dialog box permits the user to embed or link a new object of a specified class or an object based on an existing file. If the OLE controls OLETypeAllowed property is set to Linked or Embedded (rather than to Both ), however, only the specified action will be available in the Insert Object dialog box. Using Paste Special At RuntimeYou can also implement a Paste Special command in your Visual Basic program, permitting the user to paste OLE objects into an OLE control at runtime. Of course, a valid OLE object must be on the Clipboard for this method to work. Then you have two approaches. One is to use the OLE controls Paste method, which is usually executed in response to a Paste command on the applications Edit menu. The required steps are as follows:
The second and perhaps easier approach is to execute the OLE controls PasteSpecialDlg method. Executing this method displays the Paste Special dialog boxthe same one that is displayed when you select Paste Special from the OLE controls pop-up menu during program design. In this dialog box, the user selects from the As list (necessary only if more than one server is listed), chooses between the Paste and Paste Link options (to embed or link, respectively), then selects OK. If the OLE controls OLETypeAllowed property is set to Linked or Embedded, or if the object on the Clipboard permits only linking or embedding, only one of these options will be available. Inserting An Object With Drag-And-DropYou can implement a user-friendly interface that permits an object to be inserted in an OLE control with drag-and-drop. The OLE controls OLEDropAllowed property must be set to True. The user selects data in the server application, drags it to the Visual Basic application, and drops it on the OLE control. The result is the same as if the user had copied the object to the Clipboard and then executed the OLE controls Paste method. If the user is dragging an appropriate object, the mouse pointer displays a special drop icon when it is over the OLE control. Not all objects are appropriate for drag-and-drop, however. Saving And Retrieving OLE ObjectsIt is the responsibility of the Visual Basic program to save the data in its OLE controls. Neither embedded nor linked OLE objects automatically save themselves. If the user closes a form containing an OLE control, the OLE objects data is lost. This refers to the actual data with an embedded object; with a linked object, the information defining the link to the file that contains the data. For an OLE object to be available the next time the program is run, the data must be explicitly saved and then reloaded into the OLE control when the program is again executed. Capabilities that are built into the OLE control simplify this task.
|
![]() |
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. |