PDA

View Full Version : Firing a script via a rule



emcee303
02-12-2006, 05:03 PM
Hi there. I'm definitely not a coder, but I've been working with one of our developers to get some alerting via our monitoring software. At the moment we are using Outlook rules to distribute the alerts, this works well but has its limitations, eg we cannot alert based on time received. To get around that problem we came up with the script below. The problem is that this will not fire as the incoming message is re-routed before hitting the inbox. How can i add this script as a macro to fire before all other rules?
Thanks for any help

Mike.

Public WithEvents myOlItems As Outlook.Items
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
On Error GoTo Error_Trap

' If it's currently not between 7:00 A.M. and 6:00 P.M.
If Time() < #7:00:00 AM# Or Time() > #6:00:00 PM# Then
' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then
' Forward the item just received
Set myForward = Item.Forward

' Address the message
myForward.Body = StripHeader(myForward.Body)
myForward.Recipients.Add "[sms:MobileNumber]"

' Send it
myForward.Send
End If
End If

Exit Sub

Error_Trap:
' Do nothing
End Sub
Public Function StripHeader(ByVal Body As String) As String
On Error GoTo Error_Trap

' Tweak interations with this constant
Const C_RETURNS As Integer = 7

Dim I As Integer
Dim intPosition As Integer

I = 0

For intPosition = 1 To Len(Body)
If I = C_RETURNS Then Exit For
If Asc(Mid(Body, intPosition, 1)) = 10 Then I = I + 1
Next

StripHeader = Mid(Body, intPosition)

Exit_Point:
Exit Function

Error_Trap:
' Return original message if an error is encountered
StripHeader = Body
End Function

emcee303
02-12-2006, 05:06 PM
Hi there. I'm definitely not a coder, but I've been working with one of our developers to get some alerting via our monitoring software. At the moment we are using Outlook rules to distribute the alerts to various teams, this works well but has its limitations, eg we cannot alert based on time received. To get around that problem we came up with the script attached. The problem is that this will not fire as the incoming message is re-routed before hitting the inbox (it works perfectly if there aren't any rules defined). How can i add this script as a macro to fire before all other rules?
Thanks for any help

Mike.

geekgirlau
02-12-2006, 05:13 PM
Hi emcee303,

I've edited your post to put the code in VBA tags - it makes it much easier to work out what the code is doing.

When inserting code in a post, highlight all the code and click on the "VBA" button.

XLGibbs
02-12-2006, 05:43 PM
Mike, we got keep it to one thread to avoid confusion. Since this is a duplicate topic and subject, and the code appears in the other post without needing to open the attachment I have simply merged the two threads.

Thanks

Pete
PS. Sorry I have not worked out a solution yet...

MOS MASTER
02-14-2006, 12:05 PM
Hi, :hi:

Use the NewMailEx event to handle your new Email instead of a rule! (and if neccesary let it reroute your mail)

Here I put down a example:
http://vbaexpress.com/forum/showthread.php?t=4223&highlight=NewMailEx

HTH! :whistle: