10 07 WLH53SZPAF6AQL6HXFY7OVTXNSQPLTQRQNOORPI




Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Object Linking And Embedding
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




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 Browser’s usefulness for OLE automation programming is limited, because it shows all of the system’s objects—not just those that are OLE automation-capable. You cannot readily use the Browser, therefore, to locate OLE automation objects.


TIP:  Enums And Modules?
As I mentioned in Chapter 4, an Enum is a list of one or more constants in a class module, defined within an Enum...End Enum statement. Recall that this is the only way a class can make public constants available. A Module is a software component that contains only functions.


If you already know the server and class that you’ll be using, however, you can use the Browser to obtain details on the object’s methods and properties. Two buttons in the Object Browser dialog box are available to help:


•  Click on the Paste button to copy the item selected in the Members list onto the Clipboard. From there, you can paste it into your Visual Basic code.
•  Click on the ? button to view the help information related to the selected item.

Properties And Methods Of OLE Automation Objects
Like 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 object’s Insert and SaveAs methods and its Italics property. The same general principles hold for all objects. If you’re used to working with Visual Basic controls, you’ll find little difference with OLE automation objects.
Using OLE Automation
I mentioned earlier that you can use OLE automation with objects that are embedded, as well as with objects that aren’t. 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 basics—and 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.

Figure 10.8  OLEAUTO1 uses an Excel Sheet object to calculate the cube of a number.

First, the program declares a type Object variable in the form’s 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 Box’s 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 object’s SaveAs method:


XLObj.SaveAs “test.xls”


You don’t 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, here‘s 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






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. Read EarthWeb's privacy statement.



Wyszukiwarka

Podobne podstrony:
od 02 07 09 do 10 07 09
r 10 07
10 4 07
EURO W POLSCE (10 07)
143 07 (10)
5 11 10 18 07 26 47
311[10] Z1 07 Wykorzystywanie teorii błędów do opracowywania pomiarów geodezyjnych

więcej podobnych podstron