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

472 Chapter 10 AUTOMATING MICROSOFT OFFICE APPLICATIONS

The rest of the code is straightforward. When an item on the ListView control is clicked, the program recalls the selected item and displays its basic entries in the corresponding Label controls at the bottom of the screen and its body in the TextBox control (whose ReadOnly property must be set to True to prevent editing of the message). Listing 10.17 is the ListView control’s SelectedIndexChanged event handler.

Listing 10.17: Viewing a Message Item

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

Dim selID As String

If ListView1.SelectedItems.Count() = 0 Then Exit Sub txtMessage.Text = “”

selID = ListView1.SelectedItems(0).SubItems(2).Text Dim mssg As Outlook.MailItem

mssg = OLObjects.GetItemFromID(selID) txtMessage.Text = mssg.Body lblSubject.Text = mssg.Subject lblSender.Text = mssg.SenderName

lblSentOn.Text = mssg.SentOn.ToShortDateString lblRecvdOn.Text = mssg.ReceivedTime.ToShortDateString Dim i As Integer

lstAttachments.Items.Clear()

For i = 1 To mssg.Attachments.Count lstAttachments.Items.Add(mssg.Attachments.Item(i).FileName)

Next End Sub

Open the Messages project in the Visual Basic IDE to examine its code and see how it combines the items of the Contacts folder and uses them to retrieve mail items from the Inbox folder. You can modify the code to add more selection criteria or to work with different folders (for example, the Outbox folder or a subfolder under the Inbox folder).

Warning The Messages project uses the FullName property of the contacts to display the names of possible message senders. If the names you’ve used in the Contacts folder are not the same as the sender names in the incoming messages, then the program won’t select all the messages as you’d expect. There are many methods for matching contacts and messages, but they require additional effort. For example, you can use each contact’s e-mail address, which is the same in both the Contacts and Inbox folders. However, contacts may have multiple e-mail addresses, so you must make sure you search the mail items for all e-mail addresses (aliases) of the selected contact.

Recursive Scanning of the Contacts Folder

The Contacts project of the previous section assumes that all contacts are stored in the Contacts folder (likewise, the other projects assume that all messages reside in a single folder). This may be

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

PROGRAMMING OUTLOOK 473

the case for users with a small number of contacts (or totally unorganized users), but it’s not common. Most users organize their contacts in subfolders to better classify them and simplify searching. Scanning a Contacts folder with subfolders is not as simple. This operation calls for recursive programming. If you thought that the chapter on recursive programming was uncalled for in an introductory book, this is another attestation to its usefulness. The topic of recursive programming is discussed in detail in Chapter 18. In this chapter, I’ll explain the code as we go along, but if you’re totally unfamiliar with this programming technique, you should read the material on recursion first and you’ll find it easier to understand the code of this section.

VB.NET at Work: The AllContacts Project

The application that demonstrates how to recursively scan the Contacts folder is called AllContacts and can be found in this chapter’s folder on the CD. The TreeView control with the names of all subfolders under the Contacts folder is populated when the form is loaded (see Figure 10.9). Expand the various folders on the TreeView control, and click a folder’s name to see its contact items in the ListBox control on the right.

Figure 10.9

Populating the TreeView control with the names of the subfolders

The Project’s Code

Let’s start with the trivial code. First, declare the following object variables, which are used by most procedures:

Dim OutlookApp As New Outlook.Application()

Dim OlObjects As Outlook.Namespace

Dim OlContacts As Outlook.MAPIFolder

Then, in the Show All Contact Folders button’s Click event handler, enter the statements of Listing 10.18 to instantiate the OlObjects and OlContacts variables, needed to access the folders of Outlook. This code adds the root node to the TreeView control—the root node being the Contacts folder—and all the first-level subfolders under it. Each time a new subfolder is added to the TreeView control, the code calls the ScanSubFolders() subroutine, passing the current folder as argument. The ScanSubFolders() subroutine iterates through the subfolders of the folder passed as argument and adds them to the TreeView control, under the appropriate node.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

474 Chapter 10 AUTOMATING MICROSOFT OFFICE APPLICATIONS

Listing 10.18: Scanning the Subfolders of the Contact Folder

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

ByVal e As System.EventArgs) Handles bttnShowContacts.Click OlObjects = OutlookApp.GetNamespace(“MAPI”)

OlContacts = _ OlObjects.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

Dim rootNode, newNode As TreeNode rootNode = TreeView1.Nodes.Add(“Contacts”) rootNode.Tag = OlContacts.EntryID

Dim allFolders As Outlook.Folders Dim folder As Outlook.MAPIFolder allFolders = OlContacts.Folders folder = allFolders.GetFirst While Not folder Is Nothing

newNode = rootNode.Nodes.Add(folder.Name) ScanSubFolders(folder, newNode)

folder = allFolders.GetNext End While

End Sub

Later in our application, we want to be able to retrieve the contacts in any folder, when the user selects the folder. The folder’s name is not enough, because it doesn’t uniquely identify a folder. All folders have a unique ID, which you can retrieve with the EntryID property. To have this information handy later in code, we store the ID of each folder to the corresponding node’s Tag property.

The ScanSubFolders() subroutine (Listing 10.19) iterates through all the subfolders of the folder passed as argument. If any of these subfolders have subfolders of their own, the program calls the ScanSubFolders() subroutine again to scan them. This process is repeated recursively, until the initial folder has been scanned to any depth necessary. At each iteration, the code adds a new folder to the TreeView control and sets the node’s Tag property to the ID of the folder. This ID will be extracted later from the Tag property of the selected node and used to retrieve the corresponding folder.

Listing 10.19: The ScanSubFolders() Subroutine

Private Sub ScanSubFolders(ByVal currentFolder As outlook.MAPIFolder, _ ByVal currentNode As TreeNode)

Dim subfolders As Outlook.Folders subfolders = currentFolder.Folders

Dim parentNode As TreeNode = currentNode Dim newNode As TreeNode

If subfolders.Count > 0 Then Dim strFolderKey As String

Dim subFolder As Outlook.MAPIFolder subFolder = subfolders.GetFirst While Not subFolder Is Nothing

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

PROGRAMMING OUTLOOK 475

newNode = parentNode.Nodes.Add(subFolder.Name) newNode.Tag = subFolder.EntryID ScanSubFolders(subFolder, newNode)

subFolder = subfolders.GetNext End While

End If End Sub

Viewing a Folder’s Contacts

After populating the TreeView control with the structure of the subfolders under the Contacts folder, you can select a folder in the TreeView control with the mouse to display its contacts on the ListBox control at the right side of the form. When an item in the TreeView control is clicked, the AfterSelect event is triggered; the code for this is presented in Listing 10.20. This event reports the node clicked, and you can use the event’s argument to retrieve the node’s tag, which is the ID of the selected folder. Once you know the ID of the selected folder, you can create a reference to this folder (variable selFolder) and use it to scan the contact items in the actual folder.

Listing 10.20: Listing the Items of the Selected Folder

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.TreeViewEventArgs) _ Handles TreeView1.AfterSelect

If e.Node.Tag Is Nothing Then Exit Sub Dim folderid As String = e.Node.Tag Dim selFolder As Outlook.MAPIFolder

selFolder = OlObjects.GetFolderFromID(folderid) Dim itm As Integer

ListBox1.Items.Clear()

For itm = 1 To selFolder.Items.Count ListBox1.Items.Add(selFolder.Items.Item(itm).Email1Address) ListBox1.Items.Add(vbTab & selFolder.Items.Item(itm).FullName)

Next End Sub

The code displays only the contact’s name and e-mail address. You can modify the code to display any fields. For example, you can retrieve the contact’s e-mail address and send a message to all the contacts in a specific folder, as we’ll do in the last example of the chapter.

Automated Messages

The Send Message to Selected Contacts button demonstrates how to send a message once the user has picked one or more recipients. The message’s subject and body are hard-coded in this project, but you can easily modify the application so that it reads the message’s body from a text file. You

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com