Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

476 Chapter 10 AUTOMATING MICROSOFT OFFICE APPLICATIONS

can also embed keywords into the text file and replace them with the recipient’s name, title, address, and so on. The code (Listing 10.21) is short and straightforward, and it’s meant to demonstrate the basic steps for automating the creation and dispatch of electronic messages, placing them in the Outbox folder. From there, the message will leave in the same way as the messages you create manually with Outlook.

Listing 10.21: Listing the Items of the Selected Folder

Private Sub bttnSend_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnSend.Click Dim msg As Outlook.MailItem

msg = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) Dim iContact As Integer

For iContact = 0 To ListBox1.SelectedIndices.Count - 1 msg.Recipients.Add(ListBox1.Items(_

ListBox1.SelectedIndices.Item(iContact).ToString) msg.Subject = “Automated Message”

msg.Body = “Enter the message’s body here” msg.send()

Next End Sub

First, you must create a new MailItem object, variable msg, with the CreateItem method of Outlook. Then you set the fields of the MailItem object. There are many more properties you can set; they will all appear as soon as you enter the name of the variable that represents the MailItem object and the following period. Finally, call the MailItem object’s Send method to place it in the Outbox folder, from which it will be sent the next time Outlook sends and receives messages.

Summary

If you add references to the various Office applications in your projects and then open the Object Browser, you’ll realize that Word, Excel, and Outlook expose many objects, which in turn, provide numerous properties and methods. Automating Office applications is well within the reach of the average Visual Basic programmer, as long as you familiarize yourself with the objects exposed by these applications (or any other application that exposes an object model).

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Part III

Basic Framework

Classes

In this section:

Chapter 11: Storing Data in Collections

Chapter 12: Handling Strings, Characters, and Dates

Chapter 13: Working with Folders and Files

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 11

Storing Data in Collections

In this chapter, you’re going to learn how to store sets of objects to structures similar to arrays. One of the most common operations in programming is the storage and manipulation of data. There are databases, of course, which can store any type of information and preserve its structure as well, but not all applications make use of databases. If your application needs to store a few shapes (like the ones you designed in the previous chapter), or a few names and contact information, you shouldn’t have to set up a new database. A simple collection like the ones described in this chapter will suffice. Traditionally, arrays were used to store related data and objects. Since arrays can store custom data types (or structures), they seem to be the answer to many data-storage and -manipulation issues. Arrays, however, don’t expose all the functionality you may need in your application. To address the issues of data storage outside databases, the .NET Framework provides certain classes known as collections.

All collections store sets of related data, and we’ve already examined a data structure that does the same, the array. Arrays used to be indexed sets of data, and this is how we’ve explored arrays so far. In this chapter, you’re going to find out that VB.NET arrays expose useful members that make arrays extremely flexible. It took Microsoft years to get arrays right, but now you can have arrays that sort themselves, search for an element, and more. In the past, programmers spent endless hours writing code to perform the same operations on arrays, but VB.NET will free them from similar, counterproductive tasks.

There are more types of collections besides arrays, and they’re all hosted in the System

.Collections class, an assembly exposing many classes that implement collections, like the ArrayList and the HashTable collections. We aren’t going to discuss them all in this book, only the most important ones, which are the ArrayList, HashTable, Dictionary, and SortedList. Their functionality overlaps a lot, which may make it difficult to decide which structure to use. The good news is that once you’ve learned to use one of them, you can easily apply your skills to the others.

Conventional arrays are not implemented by the System.Collections class. They’re implemented by the System.Array class, which is not inheritable. This chapter starts with a discussion of the advanced features of the Array class. Once you know how to make the most of arrays,

I’ll discuss the limitations of arrays, and we’ll explore other collections that overcome these limitations.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com