Event Code for Forwarding Selected Text to Another Email Address


April 27, 2008 – 2:03 pm by JP

Here is the event code I promised for forwarding emails to another email address. It does exactly the same thing as the previous code, but since it is event code, once you place it in a class module and restart Outlook, it runs automatically without any need for you to run macros by hand.

Start by pasting the following into the ThisOutlookSession module:

VBA:
  1. Private WithEvents Items As Outlook.Items
  2.  
  3. Private Sub Application_Startup()
  4. Dim objNS As Outlook.NameSpace
  5.  
  6. Set objNS = GetNamespace("MAPI")
  7. Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
  8. End Sub

This code will set up the event handler. If you already have an Application_Startup event, simply copy and paste the inner code into it. Of course you'll want to check that you aren't duplicating any code; Option Explicit and a quick Debug>Compile will check for that.

The ItemAdd event will check any newly added items to the Inbox, and, if they meet the criteria we specify, a new mail item is created (via the Forward method) and sent to the email address of our choice. Then the original message is marked as read and neatly tucked away. Here is the complete code:

VBA:
  1. Private WithEvents Items As Outlook.Items
  2.  
  3. Private Sub Application_Startup()
  4. Dim objNS As Outlook.NameSpace
  5.  
  6. Set objNS = GetNamespace("MAPI")
  7. Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
  8. End Sub
  9.  
  10. Private Sub Items_ItemAdd(ByVal item As Object)
  11.  
  12. If item.Class = olMail Then
  13.     If Left$(item.Subject, 16) = "String to search" Then
  14.         Dim Msg As Outlook.MailItem
  15.         Dim NewForward As Outlook.MailItem
  16.         Dim MyFolder As Outlook.MAPIFolder
  17.         Dim olApp As Outlook.Application
  18.         Dim olNS As Outlook.NameSpace
  19.  
  20.       Set Msg = item
  21.       Set NewForward = Msg.Forward
  22.       Set olApp = Outlook.Application
  23.       Set olNS = olApp.GetNamespace("MAPI”)
  24.       Set MyFolder = olNS.GetDefaultFolder(olFolderInbox).Folders(”Archive”)
  25.         With NewForward
  26.             .Subject = Right(Msg.Subject, Len(Msg.Subject) - InStrRev(Msg.Subject, " "))
  27.             .To = "myemail@mobiledevice.com"
  28.             .HTMLBody = ""
  29.             .Send
  30.         End With
  31.     With Msg
  32.        .UnRead = False
  33.        .FlagStatus = olNoFlag
  34.        .Move MyFolder
  35.     End With
  36.     End If
  37. End If
  38. ExitProc:
  39. Set NewForward = Nothing
  40. Set Msg = Nothing
  41. Set olApp = Nothing
  42. Set olNS = Nothing
  43. Set MyFolder = Nothing
  44. End Sub

Paste this into the ThisOutlookSession module, save and restart Outlook to get the code to start working.

I chose some arbitrary criteria (the first 16 characters of the Subject Line), you would need to customize this for your needs.


Share and Enjoy:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Outlook, VBA, automation
Tags: , , , ,

This post has 106 views since April 27, 2008 – 2:03 pm.
  1. 4 Responses to “Event Code for Forwarding Selected Text to Another Email Address”

  2. Hi,

    Just read your post on the blog and the code samples that you have put up for VBA Outlook look very good.

    Can you guide me about writing code to delete duplicate emails from a selected folder?

    Cheers
    TG

    By Tarun Goel on Jun 6, 2008

  3. Tarun,

    Do you have any code written so far? Some of the code on the site can be rigged to do what you want. Just set an object reference to the folder like this:

    Dim MyFolder As Outlook.MAPIFolder
    Dim olNS As NameSpace

    Set olNS = Application.GetNamespace("MAPI")
    Set MyFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Folder To Search")

    Then you would have to decide how you want to identify the emails, using the Sort method to line them up so they can be compared. Are the emails exactly the same?

    Thx,
    JP

    By JP on Jun 7, 2008

  4. Thanks for this little gem!

    I just customised this to auto reply to anybody who sends me an email with a blank subject field and give them a bollocking!

    By ChrisH on Aug 12, 2008

  5. Whatever floats your boat ;)

    By JP on Aug 12, 2008

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Custom CSS tags for displaying well-formed VBA code || Contribute to this site! »