The Application’s Code VB Help

In the Form’s Load event we create two object variables, the OLApp variable, which references the Outlook application, and the mNameSpace variable, which references. Outlook’s folders. These variables are declared in the Form’s declaration section with:

the following statements:

Dim OLApp As Application
Dim mNameSpace As NameSpace

Then, the code sets up the AIlContacts variable, which references the items of the Contacts folder. This variable is also declared in the Form’s declaration section, with the following statement:

Dim AllContacts As Item

The code scans all the items in the AllContacts collection and adds the names of the contacts (property FullName) to the Combo! box. The Sorted property of the Combobox control is set to True and the code removes any duplicate entries (which will appear in consecutive positions in the list). Fcllowingis the code that contacts Outlook and sets up the All~ntacts object variable.

Initializing the Messages Project

Filtering Messages

The user can select a name-and/or a date range to limit the selected messages. If the checkboxes “From this sender” and “Between these dates” are cleared, then clicking the Show Selected Messages button will display all the messages in the InBox folder on the ListBox control. H you check either or both checkboxes, then the program will display only the messages that meet the specified criteria. To filter the messages, use the Find and FindNext methods of the AII Messages object variable. The AllMessages variable is declared as Items, and the Items object supports these two methods for retrieving selected messages. The syntax of the Find method is:

   Items.Find (filterstring)

filiterstring is an e~ion that specifies the desired criteria. The Find method returns an object, whose type depends on the type of the Items collection. H you apply the Find method to’the InBox folder, it will return a Mailltem object; if you apply it to the Contacts folder, it will return a ContactItem object. After you’ve retrieved the first item that meets the criteria, you can retrieve the remaining..ones by calling the FindNext method without specifying any arguments.

The filterstring argument is a string expression that combines field names, logicaloperators, and values. To retrieve the messages sent by Site Builder  Network, use the following string

‘[SenderName] – ‘Site Builder Network’

To retrieve all messages sent in October 1997, use the following string:

• [SentOn] a) “’10/01/97” And .[SentOn] <- “10/31/97”

(The consecutive double quotes indicate embedded double quotes, which are inserted in the string with the expression Chr(34 ).)
You can combine as many fields as needed with the usual comparison and logical operators. The field names for each item type can be found in the Object Browser. Select the desired item (e.g., MailItem, ContactItem) and look up its properties in the Members pane.

In the Messages project you use the values of various controls on the Form to build the filter string as follows. First, you validate the dates, then you build the filter string with the following statements

Capture

The filterstring variable is built slowly, according to the values entered by the user on the Form. If the-userspecifies a name, the SenderName property is set to the appropriate value. If the user specifies dates, the SentOn property is set accordingly. The filterstring variable is then passed to the Find method of the AllMessages object. Then, the program loops through the selected messages by calling the FindNext method. At each iteration, another message’s sender and subject are displayed on the ListBox control. At the same time, the selected messages are also appended to the SelectedMessages collection:

The rest of the code is straightforward. When an item on the ListBox controls clicked, the program recalls the selected itemin the SelectedMessages collection and displays its basic entries in the corresponding Label control ‘at the bottom of the screen and its body in the TextBox control (whose Locked property must be set to True to prevent editing of the message), following is the listing of the ListBox control’s Click event handler.

Viewing a Message Item

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 work with different folders (for example, the OutBox folder or a sub folder under the InBox folder.

Posted on November 6, 2015 in OLE Automation and VBA

Share the Story

Back to Top