0

I'm looking for a script to forward all incoming emails to a distribution group, but without the attachment. I need the attachment to remain on the original e-mail so it can't just be deleted. I'm using Microsoft Outlook 2010.

So just for the sake of clarity: An e-mail comes in with something attached. I want to forward this e-mail minus the attachment to a different address, while keeping the original intact.

Thanks in advance to anyone that can offer some assistance with this.

Edit: This process needs to happen automatically on every e-mail that comes in to this address.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3229528
  • 29
  • 1
  • 4
  • 8

1 Answers1

0

Put the following code into your ThisOutlookSession module:

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim varEntryID As Variant

    For Each varEntryID In Split(EntryIDCollection, ",")
        Dim objOriginalItem As MailItem
        Set objOriginalItem = Application.GetNamespace("MAPI").GetItemFromID(varEntryID)
        Dim objForwardedItem As MailItem
        Set objForwardedItem = objOriginalItem.Forward

        Do Until objForwardedItem.Attachments.Count = 0
            objForwardedItem.Attachments.Remove (1)
        Loop

        objForwardedItem.To = "toemailaddress@domain.com"
        objForwardedItem.Send
    Next
End Sub

This module can be accessed in the VBA IDE (Alt + F11) in the Project Explorer: Project > Microsoft Outlook Objects > ThisOutlookSession

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • Sorry, I should have included this in the original question so I'll edit it.. but it needs to happen automatically on every e-mail that comes in. – user3229528 Feb 13 '14 at 19:11
  • Forgive my ignorance, but how do I make this run on incoming messages? – user3229528 Feb 13 '14 at 20:22
  • It will run whenever a new message arrives because it's inside the `Application_NewMailEx` event handler. This is one of the built-in event handlers you can use in Outlook VBA from inside the `ThisOutlookSession` module. – rory.ap Feb 14 '14 at 00:23
  • It's not working. I put the code into my ThisOutlookSession module, but when an e-mail comes in nothing happens. I also made sure to change the forward to email address. – user3229528 Feb 14 '14 at 13:53
  • Do any macros work? Is Outlook set to trust macros? Check under Outlook options "Trust Center" page > "Trust Center Settings" button > "Macro Settings" page -- make sure "Enable All Macros" is selected. Also, you can run a simple test by creating a "Sub Test()" with just a Msgbox("testing") inside the sub body, then place the cursor inside the body and hit F5. If you see the dialog, macros are working. – rory.ap Feb 14 '14 at 13:58
  • You were correct, I had macros disabled. Thanks for your help! – user3229528 Feb 14 '14 at 14:29