Microsoft KB Archive/172741

{|
 * width="100%"|

-

The information in this article applies to:


 * Collaboration Data Objects (CDO), 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.

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: "Q171422 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). If you are missing this file, please see the following article in the Microsoft Knowledge Base:

"Q171440 INFO: Where to Acquire the Collaboration Data Objects Libraries" Copy the following code to a module: Option Explicit

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim objSession As MAPI.Session

Sub Main Set objSession = CreateObject("MAPI.Session") 'This example logs you into your Exchange account. 'For an example of logging onto the current user's     'default ID, see Q171422, HOWTO: Logging on to      'Active Messaging Session w/ Default Profile[mapi] objSession.Logon ("YourIDHere") Do        'Run the CheckInbox routine, then sleep 1 minute CheckInbox Sleep (60000) Loop End Sub

Private Sub CheckInbox Dim objInbox As Folder Dim objMessages As Messages Dim objMsgFilter As MessageFilter Dim objOneMessage As Message Dim i As Integer Dim iNewMessages As Integer Static dteLastItemReceived As Date    'Static variable retains 'value

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 to find 'unread messages

'Loop through the unread messages to look for new messages. For i = 1 To objMessages.Count Set objOneMessage = objMessages.Item(i) If objOneMessage.TimeReceived > dteLastItemReceived Then iNewMessages = iNewMessages + 1 End If     Next i

'If there are new messages, reset dteLastItemReceived, else 'set dteLastTimeReceived to "Now" If iNewMessages > 0 Then dteLastItemReceived = objOneMessage.TimeReceived Else dteLastItemReceived = Now End If

'Report how many new messages there are MsgBox "There are " & iNewMessages & _ " new messages since the last time I checked." End Sub