The Sample Script OSPREV.VBS

The following sample script ships with Microsoft Exchange Server version 5.5 and above. It contains the four basic functions Folder_OnMessageCreated, Message_OnChange, Folder_OnMessageDeleted, and Folder_OnTimer.

Each of these event functions calls two support functions: first GetEventDetails, and then MakeResponseMessage, as described in the following sections.

The GetEventDetails Function

In addition to error checking, the function GetEventDetails performs the following steps:

  1. Using the CDO session, it establishes the location of the current user's Microsoft Exchange mailbox. It does this with the call
    Set AMSession = EventDetails.Session.
  2. It retrieves the user's Outbox with the call
    Set fldrOutbox = AMSession.Outbox.
  3. Using the EventDetails object passed by the Microsoft Exchange Event Service, this function sets an application folder ("target" folder) to be the same as the monitored folder in which the event occurred. It does this with the assignments
    idTargetFolder = EventDetails.FolderID and
    Set fldrTarget = AMSession.GetFolder( idTargetFolder, Null ).
  4. This function also sets a target message to be the same as the message that triggered the event. It does this with the assignments
    idTargetMessage = EventDetails.MessageID and
    Set msgTarget = AMSession.GetMessage( idTargetMessage, Null ).

The MakeResponseMessage Function

In addition to error checking, the function MakeResponseMessage prepares and sends a response message that reports the number of messages in the monitored folder. For this, it uses the CDO response object in the call msgResponse.Send.

Script Text: OSPREV.VBS

<SCRIPT RunAt=Server Language=VBScript>

'---------------------------------------------------------------------
'FILE DESCRIPTION: Sample Exchange Server Event Script for Osmium
' 
' Copyright (c) Microsoft Corporation 1993-1997. All rights reserved.
'---------------------------------------------------------------------

Option Explicit 

'---------------------------------------------------------------------
' Localized strings
'---------------------------------------------------------------------
' Put all localizable strings and constants here

'---------------------------------------------------------------------
' Global Variables
'---------------------------------------------------------------------

Dim AMSession
Dim fldrOutbox
Dim msgTarget
Dim fldrTarget

'---------------------------------------------------------------------
' Event Handlers
'---------------------------------------------------------------------

' DESCRIPTION: This event is fired when a new message is added to the folder
Public Sub Folder_OnMessageCreated
    On Error Resume Next

    GetEventDetails
    If Err.Number = 0 Then
        MakeResponseMessage "A message has been added"
    Else
        Script.Response = "GetEventDetails Failed"
    End If
End Sub

' DESCRIPTION: This event is fired when a message in the folder is changed
Public Sub Message_OnChange
    On Error Resume Next

    GetEventDetails
    If Err.Number = 0 Then
        MakeResponseMessage "A message has been changed"
    Else
        Script.Response = "GetEventDetails Failed"
    End If
End Sub

' DESCRIPTION: This event is fired when a message is deleted from the folder
Public Sub Folder_OnMessageDeleted
    On Error Resume Next

End Sub

' DESCRIPTION: This event is fired when the timer on the folder expires
Public Sub Folder_OnTimer
    On Error Resume Next

End Sub

'---------------------------------------------------------------------
' Support Functions
'---------------------------------------------------------------------

' DESCRIPTION: Get the details of the event that fired
Private Sub GetEventDetails
    On Error Resume Next
            
    Dim oStores
    Dim Temp
    Dim idTargetFolder
    Dim idTargetMessage

    idTargetFolder = EventDetails.FolderID
    idTargetMessage = EventDetails.MessageID
    
    ' some of the above may not exist
    Err.Clear

    Set AMSession = EventDetails.Session
    If Err.Number = 0 Then
        ' We will send a msg, so let's get the Outbox here
        Set fldrOutbox = AMSession.Outbox
        If Err.Number = 0 Then
                Set fldrTarget = AMSession.GetFolder( idTargetFolder, Null )
                If Err.Number = 0 Then
                    Set msgTarget = AMSession.GetMessage( idTargetMessage, Null )
                    If Not Err.Number = 0 Then
                        Script.Response = "Session.GetMessage Failed: " & Err.Description
                    End If
                Else
                    Script.Response = "Session.GetFolder Failed: " & Err.Description
                End If
            Else
                Script.Response = "Session.InfoStores Failed: " & Err.Description
            End If
        Else
            Script.Response = "Outbox.Messages Failed: " & Err.Description
        End If
    Else
        Script.Response = "EventDetails.Session Failed: " & Err.Description
    End If
End Sub

' DESCRIPTION: Make a response message
Private Sub MakeResponseMessage(strSubj)
    Dim msgResponse
    Dim iMsgCount

    On Error Resume Next

    iMsgCount = fldrTarget.Messages.Count
    If Err.Number = 0 Then
        Set msgResponse = fldrOutbox.Messages.Add( strSubj, "There are now " & iMsgCount & " messages in the folder")
        If Err.Number = 0 Then
            msgResponse.Recipients.Add "", "", 1, msgTarget.Sender.ID
            If Err.Number = 0 Then
                msgResponse.Recipients.Resolve(False)
                If msgResponse.Recipients.Resolved = True Then
                    msgResponse.Send
                    If Not Err.Number = 0 Then
                        Script.Response = "Message.Send Failed: " & Err.Description
                    End If
                Else
                    Script.Response = "Recipients.Resolve Failed: " & Err.Description
                End If
            Else
                Script.Response = "Recipients.Add Failed: " & Err.Description
            End If
        Else
            Script.Response = "Messages.Add Failed: " & Err.Description
        End If
    Else
        Script.Response = "Messages.Count Failed: " & Err.Description
    End If
End Sub

</SCRIPT>