Code to load a new e-mail into a variable and then move the e-mail with vb code

Posted on 2006-04-28
Last Modified: 2010-05-01

This is my first attempt to do some vb coding within Outlook.  To get me started, I am looking for someone to provide me with some code that will do the following (including setting up any objects that need to be set):

1) Check incoming e-mails for a subject line "abc"
2) Load the e-mail into a variable
3) Upon some condition, move the e-mail to mail folder xyz

I'm hoping that from there, I can struggle through and figure out the rest!

With Regards-

PS- I am planning on the code residing within Outlook (Tools, Macros, Visual Basic Editor).  Where is the code actually stored?  I think I read that it is stored in a file?
Question by:SAbboushi
    LVL 4

    Accepted Solution

    You should put the code below in the ThisOutlookSession class (using the Visual Basic Editor)
    Please make sure you decrease the security level to the lowest (under menu: Tools -> Security)

    Private WithEvents objInboxItems As Items

    Private Sub Application_Startup()

        Dim objInbox As Outlook.MAPIFolder
        Set objInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
        Set objInboxItems = objInbox.Items
    End Sub

    Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    On Error GoTo Err_objInboxItems_ItemAdd

        Dim objNewMailItem As MailItem 'The variable holding the mail
        Set objNewMailItem = Item
        Dim objDeletedFolder As MAPIFolder
        Set objDeletedFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems)
        If InStr(objNewMailItem.Subject, "test") Then 'Check the subject
            objNewMailItem.Move objDeletedFolder 'Move to deleted items folder
        End If
        Exit Sub

        Exit Sub
    End Sub
    LVL 4

    Expert Comment

    Also make sure you restart outlook before testing the code above.

    Author Comment

    Hi paul-

    THanks for your responses.  I copied the code to the "ThisOutlookSession" object, I changed "Tools, Macros, Security" to "Low", restarted Outlook (I have 2003), sent my self an e-mail with the subject "test".  Upon receipt of the e-mail, it flashed momentarily in my inbox and then moved to the outbox.

    However, Outlook keeps closing at what seems like random moments - no message or anything, it just disappears and I have to launch Outlook again.  Any idea why?
    LVL 4

    Expert Comment

    It's kind of strange it goes out without any message at random time.
    I don't have an idea how to fix it.

    You can try using error handling also in Application_Startup() function, but I don't think it's the reason.

    If you can find something more to identify the problem, it could help me.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Title # Comments Views Activity
    Watch V6 5 42
    Excel Formula to Pull Data from Text String 7 62
    Use Multiple Forms 4 30
    Need VBS version of Excel date conversion 4 43
    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now