6

The below code (I pulled from several sources) now works in that when I receive an email with specific words in the subject line it triggers a script that runs the below.

This code then keeps the subject line, adds text the message body and the forwards to the intended recipient.

However, if the email I receive has an attachment the code no longer forwards anything. I need it to forward the attachment that was emailed to me as well (only using the code to add text to body of email otherwise I would just set a rule).

CODE BELOW:

Sub ForwardEmail(item As Outlook.MailItem)
Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
Set oExplorer = Application.ActiveExplorer

On Error GoTo Release

If oExplorer.Selection.item(1).Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject
oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "email address here"


oMail.Save
oMail.Send

End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
nfnf
  • 127
  • 2
  • 2
  • 7
  • Not yelling at all. I didn't include a "!". I just made the important part all caps to hopefully draw someones attention that was looking for questions to answer who had dealt with this before and might be willing to assist me. The written word is easy to read into in whatever way the reader wants, but I assure you that I would be most thankful for anyone's assistance. I am very new to this and will try to edit the caps out so its not misinterpreted by others. My apologies. – nfnf Mar 04 '15 at 00:54
  • It was a bit of a joke. All caps translated into yelling most of the time. I reccomend swinging by the "How to format in Markdown?" Section of the help center. – RubberDuck Mar 04 '15 at 00:56
  • Told you I was very new, I didn't even know I was being messed with. I'm the "new guy" :) – nfnf Mar 04 '15 at 01:00

2 Answers2

7

There is no need to use the Explorer object in the code:

Sub ForwardEmail(item As Outlook.MailItem)
  Dim oMail As MailItem    

  On Error GoTo Release

  If item.Class = olMail Then
     Set oMail = item.Forward
     oMail.Subject = oMail.Subject
     oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
     oMail.Recipients.Add "email address here"

     oMail.Save
     oMail.Send
  End If
 Release:
  Set oMail = Nothing
  Set oExplorer = Nothing
End Sub

You may find the Getting Started with VBA in Outlook 2010 article helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thank you very much for asssiting me. Your ammedned code was able to forward an email without an attachment, but not one with an attachment. Any further advise to try would be appreciated. – nfnf Mar 04 '15 at 14:27
  • It looks like Outlook blocked the attachment. Try to call the code outside of the rule. Do you get the same issue? – Eugene Astafiev Mar 04 '15 at 14:30
  • My macro doesn't appear in the "Run" box as an option. This is my first attempt at using vba in outlook and tried everything I could think of to get it to run. To be honest, I was a bit embarrassed I couldn't make it run without being told to by rule using script. Its strange that your amended code (thanks you BTW again for that) allows emails to be forwarded as long as there is no attachment on it, but it won't forward the email at all if it doesn't have an attachment (i.e. it doesn't forward the email without the attachment either). – nfnf Mar 04 '15 at 21:14
  • Atafiev After some researching and trial and error I have discovered that I cannot create a macro if it contains "(" after the sub name (no spaces). As soon as I type the "(" the create button becomes inactive. Also, if I don't include the "(item As Outlook.MailItem)" after the first part of the sub name, then the rule wizard doesn't recognize any script to run. Thank you for taking an interest in assisting me. – nfnf Mar 05 '15 at 13:30
  • After some researching and trial and error I have discovered that I cannot create a macro if it contains "(" after the sub name (no spaces). As soon as I type the "(" the create button becomes inactive. Also, if I don't include the "(item As Outlook.MailItem)" after the first part of the sub name, then the rule wizard doesn't recognize any script to run. Thank you for taking an interest in assisting me – nfnf Mar 05 '15 at 13:37
  • Interestingly, if the initial email is created and then the attachment is added (by clicking the paperclip and browsing) before its sent the script runs as designed. If the attachment is added via the "Save and send, as attachment" feature under file it does not work (i.e. the operation failed). The save and send fills out the subject line which I deleted and retyped for the rule to recognize but it didn't matter. So at least I have a work around. – nfnf Mar 05 '15 at 17:28
2

There is an unnecessary condition

If oExplorer.Selection.item(1).Class = olMail Then

that may cause the forwarding to be bypassed.

Sub ForwardEmail(item As Outlook.MailItem)
' Dim oExplorer As Outlook.Explorer
Dim oMail As MailItem
' Set oExplorer = Application.ActiveExplorer

On Error GoTo Release

' If oExplorer.Selection.item(1).Class = olMail Then

Set oMail = item.Forward
oMail.Subject = oMail.Subject
oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "email address here"

' oMail.Save
oMail.Send

' End If

Release:
Set oMail = Nothing
' Set oExplorer = Nothing
End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
  • I thank you for taking the time to try and assist me with this. I removed that line of code and it unfortunately still isn't working. Any other ideas would be sincerely appreciated. – nfnf Mar 04 '15 at 13:57
  • The faulty code is a false clue. If you search for a non-VBA reason there are many results. http://www.msoutlook.info/question/attachments-disappear-rich-text http://www.computerhope.com/issues/ch000728.htm https://www.novell.com/communities/coolsolutions/mystery-missing-e-mail-attachments/ http://support.microsoft.com/kb/958012 – niton Mar 04 '15 at 17:43