How to make a client rule trigger a subroutine

Posted on 2007-07-24
Last Modified: 2010-04-30
Dear experts,

Using Outlook 2003, I have set a rule that is fired when a email comes into my inbox with the word "test" in the subject. This should "run a script" (subroutine) called test() which is saved in ThisOutlookSession

Sub test(Item As Outlook.MailItem)
    MsgBox Item.Subject
End Sub

However this does not work.

What am i doing wrong? how can I get this to work?
Question by:m8rix
    LVL 19

    Expert Comment

    here's one i wrote for some other guy, you should be able to edit to do what you need

    added some extra stuff in there to be useful
    assumes the following
    c:\windows\temp exists
    if you use the 'myfolder' variable, then folder "my folder" exists under the inbox

    Sub DoStuff()
    ' Declare variables
        Dim ns As NameSpace
        Dim Inbox As MAPIFolder
        Dim Item As Object
        Dim Atmt As Attachment
        Dim File As String
        Dim i As Integer
        Set ns = GetNamespace("MAPI")
        Set Inbox = ns.GetDefaultFolder(olFolderInbox)
        Set myfolder = Inbox.Folders("my folder") 'if you want a subfolder of inbox
        i = 0
    ' Check Inbox for messages and exit of none found
        If myfolder.Items.Count = 0 Then
            MsgBox "There are no messages in the Inbox.", vbInformation, _
                   "Nothing Found"
            Exit Sub
        End If
    ' Check each message for attachments
        For Each Item In myfolder.Items 'change to inbox.items to look in inbox
            strresult = strresult & Item & vbCrLf 'item = subject
            If Item = "A Particular subject" Then
                For Each Atmt In Item.Attachments
                    'save the file to a temp location
                        File = "C:\windows\temp\" & Atmt.FileName
                        Atmt.SaveAsFile File
                        i = i + 1
                        ' here is where you add
                        ' your attachment manipulation
                        ' codes and whatnot
                        Next Atmt
            End If
        Next Item
        MsgBox strresult 'if you want to see a popup of the names of the attachments
        End Sub
    LVL 10

    Author Comment

    Thanks weellio, but I have already seen that thread when I did a search before posting my question.

    I am confident enough to write my own visual basic to do what I require.
    Please read my question again and if you think you have an answer, please tell me why my test did not work.

    Thanks for your reply
    LVL 19

    Expert Comment

    sorry was just giving you an alternative way to do things...

    your script shoudl work.

    have you tried lowering the maco security to see if it makes a difference?
    have you run the script manually theough the rules wizard "run now" option?
    when you say it "does not work", does it do anything? are there error in event viewer?
    LVL 10

    Author Comment


    > have you tried lowering the maco security to see if it makes a difference?
    Yes It is set "Low (Not recommended)"

    > have you run the script manually theough the rules wizard "run now" option?
    Yes I have but does not work.

    > when you say it "does not work", does it do anything? are there error in event viewer?
    No error, just the rule does not trigger the code

    LVL 10

    Author Comment

    Don't know what it was, but when I removed my conditions on the rule it worked.
    I have no idea what it was because my only condition was that the subject contained the word "test"
    But when I removed it it worked (triggered on ALL emails) and when I put it back it was working as desired.

    Only thing I can think of is that I misspelled the word "test" however that is highly unlikely as I remember that was one of the first things I checked????

    thanks for your help anyway, but it seems to have sorted it self out now.

    LVL 1

    Accepted Solution

    Closed, 500 points refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Email signatures have numerous marketing benefits. Here are 8 top reasons to turn your email signature into a marketing channel.
    Use email signature images to promote corporate certifications and industry awards.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now