Microsoft KB Archive/171425

= Microsoft Knowledge Base =

HOWTO: Writing an Active Messaging Inbox Agent in Visual Basic
Last reviewed: July 18, 1997

Article ID: Q171425

The information in this article applies to:


 * Active Messaging Library, version 1.1
 * Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
 * Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0

SUMMARY
This code sample demonstrates how to use Active Messaging to find out how many new, unread messages have been delivered to your Inbox since the last time this code was run.

While this task may be trivial, this code sample demonstrates the following Active Messaging tasks in Visual Basic:


 * Setting a variety of objects.
 * Using a message filter.
 * Looping through a Messages collection.

This code assumes a client session exists and is logged onto the current user's Exchange account.

For more information about starting an Active Messaging session using the current user's default profile, please see the following article in the Microsoft Knowledge Base:

ARTICLE-ID: Q171422 TITLE    : HOWTO: Logging on to Active Messaging Session with Default Profile

MORE INFORMATION
This code requires a reference to the "Microsoft Active Messaging 1.1 Object Library" (Olemsg32.dll).

Copy the following code to a module:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public objSession as MAPI.Session Copy the following code to the general declaration section of a form:

Private Sub Form_Load Set objSession = CreateObject("MAPI.Session")

'Try to logon. This line of code will log you onto the current 'session. If this fails, the most likely reason is that you do not 'have an open session. 'The error -2147221231 MAPI_E_LOGON_FAILED will return. objSession.Logon ShowDialog:=False, NewSession:=False

FindNewMessages End Sub

Sub FindNewMessages Dim objInbox As Folder             'Inbox

Dim objMessages As Messages        'Messages collection

Dim objOneMessage As Message       'One message from the message 'collection

Dim objMsgFilter As MessageFilter  'Message Filter set to                                         'Unread=True

Static dteLastItemReceived As Date 'Used to store the TimeReceived 'property of the last message in                                        'the inbox

Dim i As Integer                   'Loop counter

Dim iNewMessages As Integer        'Counter for how many new 'messages there are

iNewMessages = 0 Set objInbox = objSession.Inbox    'Set the inbox object Set objMessages = objInbox.Messages 'Set the messages object Set objMsgFilter = objMessages.Filter 'Set the Message Filter object objMsgFilter.Unread = True         'Set the filter's "Unread" 'property to true (look for                                        'unread messages only).

'Loop through all of the unread messages. Get each item in    'turn and determine if it's "TimeReceived" property is     'greater than the "TimeReceived" stored in your static 'variable "dteLastItemReceived". If it is, this is a    'new message and increment the counter. For i = 1 To objMessages.Count Set objOneMessage = objMessages.Item(i) If objOneMessage.TimeReceived > dteLastItemReceived Then iNewMessages = iNewMessages + 1 End If    Next i     MsgBox "There are " & iNewMessages & " new unread messages " & _ "since the last time I checked"

'Reset dteLastTimeSent to the TimeSent of the last message dteLastItemReceived = objOneMessage.TimeReceived

End Sub