• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

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

Hi-

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-
Sam

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?
0
SAbboushi
Asked:
SAbboushi
  • 3
1 Solution
 
paulgrunnerCommented:
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

Err_objInboxItems_ItemAdd:
   
    Exit Sub
   
End Sub
0
 
paulgrunnerCommented:
Also make sure you restart outlook before testing the code above.
0
 
SAbboushiAuthor Commented:
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?
0
 
paulgrunnerCommented:
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.


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now