HOWTO: Write a VB Active Messaging Inbox Agent

ID: Q172741

The information in this article applies to:

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:

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

   ARTICLE-ID: Q171440
   TITLE     : 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

REFERENCES

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

Additional query words: number
Keywords          : kbCDO110 kbVBp400 kbVBp500 
Version           : WINDOWS:1.1,4.0,5.0
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 8, 1999