Forwarding Selected Text to Another Email Address


April 17, 2008 – 2:22 am by JP

Greetings,

I would like to share some code I wrote last month for a user who visited my website and asked for a macro that would look for specific text in the subject of an email and forward it to another email address (in this case, an email for a mobile device). I created two versions: a Sub procedure that could be run on demand, and Event code that would automatically (in the "hands off" spirit) forward a specially crafted email.

In this post I will show the macro you would assign to a toolbar button. If you want to add this (or any other) macro to your Outlook toolbar, check out "How to assign a macro to a toolbar button" on this page. In the next post I will follow up with the Event code.

The toolbar code should be placed in a standard module in your Outlook VB Project.
First we check what type of window we are looking at by checking the ActiveWindow Property of the Application object; if it is an Explorer, that means we don't have any emails open, so we set an object reference to the first selected item. If it is an Inspector, we have an email or other item open for viewing, so we set an object reference to it. Of course, if there is no selected item, or no open item, the code fails with an error because we can't run it.

VBA:
  1. Sub FwdMailText()
  2. Dim objItem As Object
  3. Dim Msg As Outlook.MailItem
  4. Dim NewForward As Outlook.MailItem
  5. Dim MyFolder As Outlook.MAPIFolder
  6. Dim olApp As Outlook.Application
  7. Dim olNS As Outlook.NameSpace
  8.  
  9. On Error Resume Next
  10. Select Case TypeName(Application.ActiveWindow)
  11.   Case "Explorer"
  12.     Set objItem = Application.ActiveExplorer.Selection.item(1)
  13.   Case "Inspector"
  14.     Set objItem = Application.ActiveInspector.CurrentItem
  15. End Select
  16. On Error GoTo 0
  17.  
  18. If objItem Is Nothing Then
  19.   MsgBox "Nothing selected!", vbExclamation
  20.   Goto ExitProc
  21. End If

If we made it this far, we check if our object reference is to a MailItem
by checking the Class Property for the olMail constant. If it is indeed a MailItem, we set a friendly object reference "Msg" to it. I like to use string variables that easily identify what the variable represents; similar to Hungarian notation. That way I can spend more time worrying about what the code should do and less time guessing what each variable does every time I look at it. Fortunately, "Msg" is one of those non-reserved keywords that make it obvious.

VBA:
  1. If objItem.Class = olMail Then
  2.   Set Msg = objItem
  3. Else
  4.   MsgBox "No message selected!", vbExclamation
  5.   GoTo ExitProc
  6. End If
  7.  
  8. Set olApp = Outlook.Application
  9. Set olNS = olApp.GetNamespace("MAPI")
  10. Set MyFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Archive")

At this point we execute the Forward Method of the MailItem object, which creates a new email message based on the original, and returns an object reference to the about-to-be-forwarded MailItem. We set up the Subject: and To: fields, and use the Subject: from the original MailItem to create the Body of the forwarded item.

After sending the message, we move the original email to the folder called "Archive" which is one level below the default Inbox (see object reference for "MyFolder").

VBA:
  1. Set NewForward = Msg.Forward
  2.  
  3. With NewForward
  4.   .Subject = "Information You Requested"
  5.   .To = "myemail@mobiledevice.com"
  6.   .Body = Right(Msg.Subject, Len(Msg.Subject) - InStrRev(Msg.Subject, " "))
  7.   .Send
  8. End With
  9.  
  10. With Msg
  11.   .UnRead = False
  12.   .FlagStatus = olNoFlag
  13.   .Move MyFolder
  14. End With
  15.  
  16. ExitProc:
  17. Set NewForward = Nothing
  18. Set Msg = Nothing
  19. Set objItem = Nothing
  20. End Sub

Here is the code in its entirety:

VBA:
  1. Sub FwdMailText()
  2. Dim objItem As Object
  3. Dim Msg As Outlook.MailItem
  4. Dim NewForward As Outlook.MailItem
  5. Dim MyFolder As Outlook.MAPIFolder
  6. Dim olApp As Outlook.Application
  7. Dim olNS As Outlook.NameSpace
  8.  
  9. On Error Resume Next
  10. Select Case TypeName(Application.ActiveWindow)
  11.   Case "Explorer"
  12.     Set objItem = Application.ActiveExplorer.Selection.item(1)
  13.   Case "Inspector"
  14.     Set objItem = Application.ActiveInspector.CurrentItem
  15. End Select
  16. On Error GoTo 0
  17.  
  18. If objItem Is Nothing Then
  19.   MsgBox "Nothing selected!", vbExclamation
  20.   Goto ExitProc
  21. End If
  22.  
  23. If objItem.Class = olMail Then
  24.   Set Msg = objItem
  25. Else
  26.   MsgBox "No message selected!", vbExclamation
  27.   GoTo ExitProc
  28. End If
  29.  
  30. Set olApp = Outlook.Application
  31. Set olNS = olApp.GetNamespace("MAPI")
  32. Set MyFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Archive")
  33.  
  34. Set NewForward = Msg.Forward
  35.  
  36. With NewForward
  37.   .Subject = "Information You Requested"
  38.   .To = "myemail@mobiledevice.com"
  39.   .Body = Right(Msg.Subject, Len(Msg.Subject) - InStrRev(Msg.Subject, " "))
  40.   .Send
  41. End With
  42.  
  43. With Msg
  44.   .UnRead = False
  45.   .FlagStatus = olNoFlag
  46.   .Move MyFolder
  47. End With
  48.  
  49. ExitProc:
  50. Set NewForward = Nothing
  51. Set Msg = Nothing
  52. Set objItem = Nothing
  53. End Sub

For the Body property, I chose to include the text from the subject line (everything starting from the left all the way up to the first space) of the original item, but you can include anything you want. If you wanted to include the original message body in the forward, try this:

.Body = Msg.Body

If you are curious about the properties and methods of the MailItem object, you can enter a new line right below "With NewForward" and type a period "." You should get a dropdown list of everything you can do with a MailItem, courtesy of Intellisense. Once you select one, you can press F1 for assistance and sample code. This is a great way to learn about the properties of an object; just declare an object of a specific type, then start typing "objectname." and you get that list. Just be sure you go to the Options in the VB Editor in Outlook (press Alt-F11, then go to Tools>Options) and on the Editor tab, check all the boxes in the "Code Settings" frame.

Enjoy,
JP


If you enjoyed this page:
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
Tags: , ,

Post a Comment

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


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





Subscribe without commenting

Keep Reading:

Browse Posts:


« Hands Off That Email Attachment! || Custom CSS tags for displaying well-formed VBA code »