Automatically Forward Emails and Remove/Replace All or Part of Body

Status
Not open for further replies.
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Hi! I'm trying to use a combination of a rule and macro to automatically forward emails I receive with a specific subject and delete or remove two hyperlinks in the body. I primarily want to forward an attachment and don't really mind what happens to the body. If removing the whole body is easier, that's ok.

The rule portion is working fine, but I can't seem to manipulate the body of the automatically forwarded email using any of the VBA I've found on this forum or others. I'm not even sure if the macro is running to be honest! Maybe there's an easier way to accomplish this, such as having one macro forward an email and alter the body rather than trying to use a rule?

Below is the email. The subject will always be "Monthly Local SEO Report," and I'm trying to remove the two hyperlinks at the bottom of the body.

I'm completely new to VBA and macros, only having run a few I found online in Excel, so any help would be greatly appreciated, thank you!

From: Local SEO Notification <bcarson@thinkstrategic.com>
Sent: Friday, June 8, 2018 2:14 PM
To: bcarson@thinkstrategic.com
Subject: Monthly Local SEO Report

An analytics report has been shared with you by Local SEO
has shared a Local SEO Analytics report. It's attached to this email.

Sincerely,
Local SEO Local Search Management

Local SEO
update notification preferences
 
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Here's the script I'm using

Code:
Sub myRuleMacro(item As Outlook.MailItem)
End Sub

Sub RemoveExpression()

Dim outNS As Outlook.NameSpace
Dim outFldr As Outlook.Folder
Dim outMailItems As Outlook.Items
Dim outMailItem As Outlook.MailItem




    Set outNS = Application.GetNamespace("MAPI")
    Set outFldr = outNS.GetDefaultFolder(olFolderDrafts)
    Set outMailItems = outFldr.Items
    
    For Each outMailItem In outMailItems
        
        outMailItem.Body = Replace(outMailItem.Body, "Local SEO", "")
    Next
    
    Set outMailItems = Nothing
    Set outFldr = Nothing
    Set outNS = Nothing
    
End Sub
Here's a screenshot of my rule
1528906071398.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This is the problem - this is used to process every message in the folder and you don't do this with rules.
For Each outMailItem In outMailItems
outMailItem.Body = Replace(outMailItem.Body, "Local SEO", "")
Next

Plus the rule wasn't doing anything:
Sub myRuleMacro(item As Outlook.MailItem)
End Sub


I didn't test this so i might have made a typo, but this is basically what you need - the item is identified by the message the rule is processing and is referenced using Item : (item As Outlook.MailItem)

Code:
Sub myRuleMacro(item As Outlook.MailItem) 
        item.Body = Replace(Item.Body, "Local SEO", "")
item.save
End Sub
 
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Thank you very, very much for the reply! Yes, I had noticed I hadn't set up the script properly yesterday and redid it, still to no avail. I tried your code just now, and still am not having any luck. It's weird the way the rule is ordered. It looks like running the script comes after forwarding the email, but I need the text replaced prior to forwarding it and don't see a way to change the order. Maybe the events are simultaneous? Regardless, below is the script and rule I'm using now. I've simplified the rule since I screen-shot it last to avoid over-complicating it at this stage. I'm still getting "Local SEO" in the body of the mail when I receive it and when it gets autoforwarded by the rule. I noticed in a few similar threads that HTML could be an issue. Do I need to use a different function?

1528986232433.png


Code:
Sub myRuleMacro(item As Outlook.MailItem)
        item.body = Replace(item.body, "Local SEO", "")
item.Save
End Sub
Again, thank you very much for your time!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You can't use other actions in the rules along with run a script.

This will remove the links from the forward copy and forward it.

Code:
Sub myRuleMacro(item As Outlook.MailItem)
Set myForward = Item.Forward
myForward.Recipients.Add "alias@domain.com"

        myForward.body = Replace(myForward.body, "Local SEO", "")

myForward.Send

End Sub
 
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Thank you very much for the reply. I've updated the script and removed all other events from the rule; however, it looks like Outlook is having trouble running the script now.

1529596305726.png


Here's a screen shot of it:

1529596356421.png


And of the rule:

1529596386925.png


Any feedback would be greatly appreciated. Again, thank you so much!

1529596342782.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Did you try re-selecting the script? It looks like you either moved the script to a module or renamed the module
 
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Did you try re-selecting the script? It looks like you either moved the script to a module or renamed the module
Yes, I'm not quite sure what the issue was, but it wasn't quite that. After messing around endlessly, I have gotten it to work; however, it appears that the incoming message is getting forwarded twice. I don't believe this is an issue with the rule or script, as it works when I trigger it by mailing myself, but when I receive an email from a reporting system that triggers the rule it gets double forwarded. Would you happen to know a potential cause of that or how to prevent the same message from getting forwarded twice?
 
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Please disregard the previous post as I've resolved the issue. I wouldn't have been able to do it without you. Thank you very much!
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
L Automatically forward ALL outgoing/sent emails to another email adddress (not just for a specific distribution list) Using Outlook 2
J Automatically forward email and apply template Outlook VBA and Custom Forms 0
O Forward a email with modified body Automatically. Outlook VBA and Custom Forms 0
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
A Outlook 2010 How to automatically forward only legitimate messages - not spam, to another exchange a Using Outlook 1
C Meeting forward notifications in Outlook 2010 or 2007 automatically going to junk e-mail folder Using Outlook 1
D Custom Search Folders not refreshing/updating automatically Using Outlook 0
M Automatically add senders first name to a greeting Outlook VBA and Custom Forms 1
C Add Form to Appointments Received, Automatically Outlook VBA and Custom Forms 6
Y Outlook 2013 Stop Outlook from automatically assigning categories to Tasks Using Outlook 0
A How to open a specific link automatically with outlook 2016 Outlook VBA and Custom Forms 5
P Automatically Categorize Meetings once they are accepted Outlook VBA and Custom Forms 5
W Automatically open attachments without automatically printing them Using Outlook 0
N How to set automatically the default or user defined Quickstyle Templates by Answer in Outlook Using Outlook 1
O Run macro automatically at sending an email Using Outlook 11
D Outlook 2016 automatically increment anniversaries Using Outlook 1
T Office 2013 no longer updating automatically Using Outlook 2
D Print attachments automatically and moves the mail to a new folder Outlook VBA and Custom Forms 9
A How to open a specific link automatically with outlook Outlook VBA and Custom Forms 13
L Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 33
N how to sync automatically when outlook opens Using Outlook 10
A Sort emails into subfolders based on sender and deleting emails automatically Outlook VBA and Custom Forms 3
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
R Make Enter Network Password Prompt Go Away Automatically Using Outlook 0
I Print Automatically Attachments Outlook VBA and Custom Forms 3
S Automatically selecting folders and deleting messages in Outlook VBA Outlook VBA and Custom Forms 7
M Outlook 2016 Rules Not Working Automatically Using Outlook 5
Diane Poremsky Automatically create a task when sending a message New Slipstick.com Articles 0
D Is it possible to automatically send an email when it is moved to a folder? Exchange Server Administration 1
A Automatically send email based on drop-down field? Outlook VBA and Custom Forms 2
M Automatically create event in calendar when task is created Outlook VBA and Custom Forms 1
Diane Poremsky Create Appointment From Email Automatically New Slipstick.com Articles 0
Cameron Piper Automatically update custom forms across multiple computers Outlook VBA and Custom Forms 1
T Automatically open link in email received Outlook VBA and Custom Forms 33
N Automatically file email messages Using Outlook 2
J Automatically Move Old Items from a Shared Mailbox to a .PST on a Network Drive Outlook VBA and Custom Forms 1
M Change default "automatically test account settings when Next is clicked" Using Outlook 1
D Automatically file message and reply to folder. Using Outlook 1
A Code to automatically delete message after sending Outlook VBA and Custom Forms 6
Diane Poremsky Automatically Create Contacts From Messages New Slipstick.com Articles 1
T Outlook 2010 emails do not refresh automatically Exchange Server Administration 1
L Save Mail Message to new folder created automatically Outlook VBA and Custom Forms 1
G Hae Rules NOT perform automatically? Using Outlook 1
F Automatically close email after selecting mark unread Using Outlook 1
nathandavies How to save emails to a specific folder on a network automatically using a macro Outlook VBA and Custom Forms 14
T Default inbox not automatically sending/receiving Using Outlook 1
Angger Microsoft Outlook 2010 Automatically Restarting Using Outlook 2
N Outlook Shared appointments automatically to Private Calendar Using Outlook 1
M Automatically move Outlook message to the specific folder when click on reply to all Using Outlook 4
Similar threads


















































Top