HOWTO: Write a VB Active Messaging Inbox Agent

Last reviewed: November 21, 1997
Article ID: Q172741
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:

   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 Active Messaging 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 : ActMsg
Version : WINDOWS:1.1,4.0,5.0
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.