1

I take code from different sources and put them together.

I want to manually categorize emails, manually select the ones I want to send, then automatically forward the selected emails to the appropriate party based on the category.

I've been using two sources to try to make this work:

https://www.datanumen.com/blogs/auto-move-emails-different-color-categories-different-folders-outlook/

Forward Email with its attachment in Outlook 2010

Here is what I put together. When I run it, nothing happens:

Sub ForwardMSREmail()

    Dim oExplorer As Outlook.Explorer
    Dim oMail As MailItem
    Set oExplorer = Application.ActiveExplorer

    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

        Dim objMail As Outlook.MailItem

        If InStr(objMail.Categories, "Joshua") > 0 Then
            oMail.Recipients.Add "email address"
            oMail.Save
            oMail.Send
        End If

    End If

    Release:
    Set oMail = Nothing
    Set oExplorer = Nothing

End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Joshua
  • 11
  • 2
  • What is 'item' meant to be? You're not setting its value anywhere. Also, as a side note, you really don't want to be sending actual mail via macros, especially while you're testing. Instead, you should save it as a draft, then send the draft manually. If you were to mess up your while testing, you could potentially spam your entire company. – Josh Eller Aug 06 '18 at 19:59
  • I'm not 100% sure I know exactly what you're referencing. – Joshua Aug 06 '18 at 20:05
  • @JoshEller That would be scary, can you tell me how I could set up the draft idea you mentioned? – Joshua Aug 06 '18 at 20:06
  • Could you just use the Outlook Rules Wizard to do this for you? I like VBA, but sometimes it is easier when you don't have to recreate the wheel... :-) https://www.lifewire.com/forward-outlook-mail-1170648 – AdamsTips Aug 06 '18 at 20:09
  • The line: "If item.Class = olMail Then" isn't doing anything. "item" is a variable, but you haven't set its value, so it's class will never be equal to olMail. You'll need to figure out how to actually get the mail items you want to forward. In terms of not sending mail, just get rid of the line "oMail.send". The line before it will save it to your drafts folder. – Josh Eller Aug 06 '18 at 20:09
  • @AdamsTips I found two issues when looking through Rules first. I have to clean/sort the inbox before sending as it receives a lot of junk/spam. Also, the people i'll be sending it to differs by day and what emails are received. I wish it was less complicated, believe me! – Joshua Aug 06 '18 at 20:15
  • @JoshEller Thanks for the tip on the drafts, I'll definitely need to change it before I annoy the wrong exec! Do you think it would be possible to set the Item as emails I've highlighted in that inbox? Is that what you mean by "get the mail items you want to forward?" – Joshua Aug 06 '18 at 20:36
  • 1
    The reason for "when I run it, nothing happens" is due to `On Error GoTo Release`. Remove this line to see the errors. – niton Aug 07 '18 at 20:38

0 Answers0