![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
You may have noticed that many of the items listed in the Object Browser and the References list seem to be related to the various custom controls that are part of Visual Basic. This reflects the fact that Visual Basic controls are implemented using OLE/ActiveX technology. Unfortunately, the Object Browsers usefulness for OLE automation programming is limited, because it shows all of the systems objectsnot just those that are OLE automation-capable. You cannot readily use the Browser, therefore, to locate OLE automation objects.
If you already know the server and class that youll be using, however, you can use the Browser to obtain details on the objects methods and properties. Two buttons in the Object Browser dialog box are available to help:
Properties And Methods Of OLE Automation ObjectsLike other Visual Basic objects, OLE automation objects are manipulated by means of their properties and methods. Of course, what you can do with an object depends on the object itself, but the fundamental procedure is similar to working with Visual Basic controls. Each object has its own set of methods and properties. For example, if you created a word-processing document object called MyObj, you could manipulate it as follows: MyObj.Insert Dear Mr. Gates: Insert some text. MyObj.Italics = True Make it italics. MyObj.SaveAs LETTER_TO_BILL.DOC Save to disk. This code uses the objects Insert and SaveAs methods and its Italics property. The same general principles hold for all objects. If youre used to working with Visual Basic controls, youll find little difference with OLE automation objects. Using OLE AutomationI mentioned earlier that you can use OLE automation with objects that are embedded, as well as with objects that arent. In this section, I will demonstrate both types of OLE automation. These are simple demonstrations, not the sort of tasks that OLE automation would be required to perform. Again, my purpose is to demonstrate the basicsand for that purpose, these programs serve perfectly well. OLE Automation With A Nonembedded Object For the nonembedded object, I will use an Excel Sheet object. To run this program, you must have Microsoft Excel installed on your system, although other Windows spreadsheet programs may support OLE automation. If you have another spreadsheet program, look through its documentation to see if it supports OLE automation and identify the corresponding methods and properties. This project, called OLEAUTO1.VBP, is shown executing in Figure 10.8. It has one form that contains two Text Boxes, two Labels, and one Command Button. Enter new Caption properties for the Form, the Labels, and the Command Button, as shown in the figure, deleting the Text property values of the two Text Boxes. Otherwise, the object properties can be left at their default values. Be sure to put the Enter a number label next to the Text Box named Text1.
First, the program declares a type Object variable in the forms General Declarations section. The Form_Load event procedure performs the two required initialization steps: creating the Excel Sheet object, and outputting the calculation formula in cell A1 of the spreadsheet object. The two lines of code are shown here: Set XLObj = CreateObject(Excel.Sheet) XLObj.Worksheets(Sheet1).Range(A2).Formula = =A1^3 The spreadsheet is now set up to perform the desired calculation. Because the formula you entered in the spreadsheet refers to cell A1, you must place the input value there. You want the calculation to be performed when the user enters or changes a number in the Enter a number Text Box. Place the needed code in the Text Boxs Change event procedure. Once you send the input value to cell A1 of the spreadsheet, the Sheet object immediately performs the calculation, and the answer is waiting to be retrieved from cell A2. Place that value in the second Text Box, and the code is complete: Clear the results text box. Text2.TEXT = Put the input value in cell A1 of the spreadsheet. XLObj.Worksheets(Sheet1).Range(A1).Value = Val(Text1.Text) Retrieve the answer from cell B1. Text2.Text = XLObj.Worksheets(Sheet1).Range(A2).Value To save the spreadsheet to disk, you would execute the objects SaveAs method: XLObj.SaveAs test.xls You dont want to save the file, however, so all you need to do before quitting the program is to destroy the object: Set XLObj = Nothing Listing 10.3 gives the complete code for OLEAUTO1.FRM. Listing 10.3 Code in OLEAUTO1.FRM. Option Explicit Dim XLObj As Object Private Sub Command1_Click() If we wanted to save the object, heres how. XLObj.SaveAs filename.xls. Destroy the object. Set XLObj = Nothing Exit the program. End End Sub Private Sub Form_Load() Create the Excel sheet object. Set XLObj = CreateObject(Excel.Sheet) Put the cube formula in cell B1. XLObj.Worksheets(Sheet1).Range(A2).Formula = =A1^3 End Sub Private Sub Text1_Change() Clear the results text box. Text2.Text = Put the input value in cell A1 of the spreadsheet. XLObj.Worksheets(Sheet1).Range(A1).Value = Val(Text1.Text) Get the answer from cell B1. Text2.Text = XLObj.Worksheets(Sheet1).Range(A2).Value 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. Read EarthWeb's privacy statement. |