The AutoMssg.application demonstrates how to create e-mail messages from within your Visual Basic applications and use Outlook to deliver them. It also shows another technique for retrieving and organizing contacts. Initially, the application retrieves the names of the companies only and djsplays them in’ the Comp~es list. The user can select a company and see the contacts in this company in the Contacts list. To add new names to the list-of recipients, the user must double-click their names in the Contacts. The list of recipients is sorted, but it. doesn’t prevent duplicate entries. The reason for this is that you’ll find “Smiths” in every ~ttrer company, so eliminating duplicate entries based on names is nota. good idea. Displaying more information to make the entries in the recipients list’ unique would clutter the display. So, for this project, I’ve decided to use a sorted ListBox control so that duplicate names appear together and allow the User to decide which ones to select. The AutoMssg project’s code is straightforward and I will not include its listing here. You can open the project in the VISual Basic IDE and see how it prepares the messages.
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). ‘Thismay be 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 simplify searching. Scanning a Contacts folder with subfolders not as simple. This operation calls for recursive programming. If you thought that . Chapter 11,Recursive Programming, was uncalled for in an introductory book, this is another attestation to the usefulness of recursive programming. It’s not an obsession of mine, but some tasks just can’t be carried out without recursion. Nor could I have skipped these tasks either. You may find the related programming techniques difficult, but without them you wouldn’t be able.to recursively scan the Contacts folder if it contained subiolders Gust as you wouldn’t be able to scan a TreeView node with child nodes nested to multiple levels).
VB6 at Work: The AIiContacts Project
The application that demonstrates how to recursively scan the Contacts folder is called AllContacts and can be found in the ALLCONTS folder in this chapter’s folder on the CD. When the application’s Form is loaded, click the Populate Tree button to populate the TreeView control with the names of all subfolders under the Contacts folder (as shown in Figure 14.22). 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.
The Project’ Code
Let’s start with the trivial code First. dectare are tile following object variables, which are used by most procedures:
Then, in the Form’s Load event, enter the following statements to create a new instance of Outlook and the object variables needed to access the folders of Outlook.
Creating an Outlook Object Variable
The OutlookApp variable represents the application. The OIObjects variable represents alljhe folders exposed by Outlook and OIContacts represents the Contacts folder. The declarations of these variables are:
Public 010bjects As outlook.NameSpace
Public OlContacts As outlook.MAPIFolder
The core of the application is the Populate button’s Click event handler, which populates the Tree View control recursively.
Populating the TreeView control
The loop of Code 14.26scans each item in this collection and adds the name of the current folder to the TreeView control. The new’ node becomes a child node of the Contacts node, and its key is the EntryID property of its parent node.
Then, w~must create another collection, the Folders collection, with the current folder’s subfolders, This collection’s items are added to the TreeView control and then we call the ScanSubFolders() subroutine to scan the sub folders of each item. The code of the ScanSubFoldersO subroutine is shown next.
This subroutine’s code is quite simple (for a recursive procedure, that is). It creates a collection of the current folder’s subfolders, the subFolders collection. Then, it scans each item in the collection, adds ‘its name to the TreeView control, and scans it recursively, which means it calls itself passing the name of the current folder as argument.
Viewing a Folder’s Contacts
After populating the ‘IieeView control with the structure of the subfolders under the ‘Contacts folderfYou can select a folder in the TreeView control with the mouse to display its contacts on the ListBox control in the right-hand side of the Form. When an item in the TreeView control is clicked, the NodeClick event is triggered. This event reports the node clicked, and you can use the event’s argument to retrieve the node’s key, which is the 10 of the Selected folder. Once you kriow the ID of the selected folder, you can create a reference to this foldez (variable thisFolder) and use it to scan the contact items in the actual folder. The code of the TreeView control’s NodeClick event is shown next.
Listing the Items of the Selected Folder
The code displays only the contact’s LastName and FirstName properties. You can modify the code to display any fields. For example, you can retrieve the contact’s e-mail address and send automated messages as we did in the previous example. Uyou think these examples are interesting and you have good reason to automate Outlook; you should try to incorporate the code of the AllContacFs project into f:he.Contacts project.
The introduction to the topic of OLE Automation ends with this example. Ifyou add references to the various Office applications in your projects and then open the Object Browser, you’ll realize that the Office applications expose many objects, which in turn, provide numerous properties and methods. Automating Office applications with VBA is well within the reach of the average VISual Basic programmer, as long as you familiarize yourself with the objects exposed by the Office applications (or any other application that supports VBA). After reading and studying this chapter, you should know the basics of VBA programming and understand that it’s not fundamentally different from Visual Basic programming.