Outlook stores different types of information in different files. Contact information is stored in the Contacts file, incoming messages are stored in the InBox folder, and so on. Most users, however, customize Outlook’s folder structure by adding subfolders to the default folders. To organize your contacts, for’instance, you can create Business and Personal subfolders under the Contacts folders. Likewise, you can create Busniss, Personal, and Junk folders under the InBox folder. One of the most common programming tasks in automating Outlook is the extraction of the desired information. In the following examples, you’ll see how to access the contacts and incoming messages in the Contacts and InBox folders, respectively.
VB6 at Work: The Contacts Project
The first example of how to OLE automate Outlook is the Contacts application, whose Form is shown in Figure 14.20. The Contacts application assumes that all contact items are .stored in the Contacts folder. If you’ve organized your contacts differently, perhaps in subfolders under the Contacts folder, copy a few contacts temp’0rarily to the Contacts folder so that you can test the application. Later, in the section “Recursive Scanning of the Contacts Folder,” you’ll see how you can scan the entire Contacts folder recursively, including its subfolders. The Contact project’s main Form contains two lists. The first list is populated with company names, which are read from the contact items. This list doesn’t contain any duplicate entries, even though a typical Contacts folder contains multiple contacts from the same company. To view the contacts in a company, click the company’s name and the corresponding contacts will appear in the Contacts list. Then, each time you click a contact name, more information about the selected contact will be displayed in the lower .half of the Form.
The program starts by contacting Outlook when the Start Outlook button is clicked. The button’s codeis shown next.
If Outlook is contacted successfully, the View Contacts button is enabled. When this button is clicked, the program creates a collection with all the items in the Contacts folder and sorts the allContacts collection according to the company name. Duplicate company names are removed from the list with the If statement at the end of the subroutine. The code of the View Contacts button follows:
Populating the Contacts List
Displaying Contact Information
VB6 at Work: The Messages Poject
The Messages project demonstrates some techniques for retrieving mail items. Messages are stored in the InBox and OutBox folders, as well as any custom folders created under these two folders by the user. The Messages example retrieves the messages from the InBox folder only. If you don’t have any messages in this folder, temporarily move some incoming messages from your custom folders to the InBox folder to test the application. Later 4′ the chapter, you’ll see how to retrieve all the messages under InBox, including its subfolders nested-to any depth. If you’ve read the previous chapters, you probably know you’re facing another recursive procedure.
The Messages application (shown in Figure 14.21) lets you select messages based on their sender or the date they were sent. The user can specify the criteria’ with the controls on the’ upper-right section of the Form and then click the Show Selected Messages button to display the messages that meet the criteria in the Selected Messages ListBox. Theprogram displays only each message’s sender and subject. Then, each time the user selects a message on this list by clicking a message item, more information about the selected message is displayed in the controls in the lower half of the Form (including the message’s body). Ifthe message contains attachments, the names of the attached files are displayed in a message box. Run the project and experiment with it. There are two issues you should be aware of. First, the . Messages application can see only the messages in the InBox folder. If you’ve organized your messages into subfolders under the InBox folder, you must move a few messages to the InBox folder temporarily. The second issue is that the sender names are’read from the Contacts folder. If the names in the Contacts folder don’t match the names that appear in the messages, then you won’t see the messages sent by the selected contact.