[ Pobierz całość w formacie PDF ]
.Application object to check the spelling of each word.The loop shown in Listing 10.8 goesthrough each word in a list and checks its spelling.If the word is misspelled, it s added to a secondListBox control.This application uses the Word.Application object and doesn t create a documentwith the text to be spell-checked.Listing 10.8: Spell-Checking a List of WordsDim WordApp As New Word.application()Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickDim SpellCollection As Word.ProofreadingErrorsDim wrd As IntegerListBox2.Items.Clear()Dim words As Integer = ListBox1.Items.CountFor wrd = 0 To words - 1Me.Text = Spelling. & CInt(wrd / words * 100) & % doneIf Not WordApp.CheckSpelling(ListBox1.Items(wrd)) Thenwww.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c10.qxd 11/11/01 4:17 PM Page 453PROGRAMMING EXCEL 453ListBox2.Items.Add(ListBox1.Items(wrd))End IfApplication.DoEvents()NextEnd SubProgramming ExcelExcel is probably the most popular application among users and developers.Actually, there arecountless programmers who earn their living by doing everything with Excel they even write lim-ited database applications using Excel as a data store.I m not suggesting you start using Excel as auniversal tool, but it s very likely that, at some point, you ll be called to import data from an Excelspreadsheet into your applications.In most situations, the tabular format of Excel isn t what youreally need, and you ll have to write code to import the data into your applications.Another good reason for using Excel s object model is to format and print tabular data.An appli-cation that generates reports on a regular basis (every day, for example) can create XLS files that canbe used for neat printouts, as well as for archiving purposes.To use Excel s object model in your code, you must add a reference to the Microsoft Excel 9.0 ObjectLibrary item to your project.Open the Project menu, select Add Reference, and double-click the nameof the Excel library.Then click OK to add an instance of Excel s object model to your application.Tocontact Excel from within your VB application, declare a variable of the Excel.Application type.Thefollowing declaration must appear outside the procedures that use it:Dim EXL As New Excel.ApplicationEXL represents a new instance of Excel, which runs in the background.To access Excel s functionality, you can use a hierarchy of objects, which are described next.Theobjects that Excel exposes have different names from those of Word, but they form an equally sensi-ble and structured hierarchy for accessing data stored in a tabular arrangement.Just as Word s basicunit of information is the text segment (not characters or words), Excel s basic unit of information isalso called Range.A Range object can contain a single cell or an entire worksheet (and everything inbetween).Two important methods of Excel s Application object are the Calculate method, which recalcu-lates all open worksheets, and the Evaluate method, which evaluates math expressions and returnsthe result.The following statement returns a numeric value that is the result of the math expressionpassed to the Evaluate method as argument:Dim result As Doubleresult = EXL.Evaluate( cos(3/1.091)*log(3.499) )You can also use variables in your expressions as long as you store their values in specific cellsand use the addresses of these cells in the expression.You will see shortly how to assign formulasto specific cells.Doing so allows you to calculate complicated expressions that involve other cellsas well.www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA2877c10.qxd 11/11/01 4:17 PM Page 454454 Chapter 10 AUTOMATING MICROSOFT OFFICE APPLICATIONSThe Worksheets Collection and the Worksheet ObjectEach workbook in Excel contains one or more worksheets.The Worksheets collection, which issimilar to Word s Documents collection, contains a Worksheet object for each worksheet in the cur-rent workbook.To add a new worksheet, use the Add method, whose syntax is as follows:Application.Worksheets.Add(before, after, count, type)The before and after arguments let you specify the order of the new worksheet in the workbook.Youcan specify one of the two arguments; if you omit both, the new worksheet is inserted before theactive worksheet (and also becomes active).The type argument specifies the new worksheet s type andcan have one of the values in Table 10.1.Table 10
[ Pobierz całość w formacie PDF ]