Auto Run VBA Code on new email

crashoz

New Member
Outlook version
Outlook 2019 64-bit
Email Account
Office 365 Exchange
Hi, I have found a very useful VBA code to remove a line of text in an email but I need to run it manually for it to work.

What would I need to add in order to have it automatically run each time i receive a new email in my inbox?

Code:
Option Explicit

Sub RemoveExpressionFOLDER()

Dim outFldr As Folder
Dim outItems As Items
Dim outMailItem As MailItem

Dim i As Long
Dim cleanCount As Long

Set outFldr = ActiveExplorer.CurrentFolder

Set outItems = outFldr.Items

For i = 1 To outItems.Count

    If outItems(i).Class = olMail Then

        Set outMailItem = outItems(i)

        With outMailItem

            'Debug.Print .Subject

            If InStr(.body, "words to remove here ") Then

                If .BodyFormat = olFormatHTML Then
                    .HTMLBody = Replace(.HTMLBody, "words to remove here", "")
                Else
                    .body = Replace(.body, "words to remove here", "")
                End If

                .Save

                cleanCount = cleanCount + 1

             End If

          End With

     End If

    Next i

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
To use it manually, this line needs to go:
Set outMailItem = outItems(i)
replace with
Set outMailItem = objApp.ActiveExplorer.Selection.Item(1)

once you do that, these lines won't be doing anything
Set outFldr = ActiveExplorer.CurrentFolder
Set outItems = outFldr.Items
For i = 1 To outItems.Count
If outItems(i).Class = olMail Then

This:
cleanCount = cleanCount + 1

or these:
End If
Next i

To run it when mail arrives, you can either use a rule or an itemadd macro that watches the folder.

For a rule, you need to change the name name line to this:
Public Sub RemoveExpressionFOLDER(outMailItem As Outlook.MailItem)
and remove all of the lines mentioned above.

To test the script on a selected message (or just to use it manually), select a message and run this macro. It calls the run a script rule.

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
RemoveExpressionFOLDER objItem

End Sub

Outlook's Rules and Alerts: Run a Script (slipstick.com)

How to use an ItemAdd Macro (slipstick.com)

Outlook VBA: Work with Open Item or Selected Item (slipstick.com)
 
Similar threads
Thread starter Title Forum Replies Date
D auto forward base on email address in body email Outlook VBA and Custom Forms 0
M Replyall macro with template and auto insert receptens Outlook VBA and Custom Forms 1
R Auto Forwarding with different "From" Outlook VBA and Custom Forms 0
P auto-complete is hopelessly broken Using Outlook 0
R Auto Assign Category colours to Incoming Emails based on whom the email is addressed Outlook VBA and Custom Forms 3
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
V Auto-Submitted: auto-replied in header Using Outlook 0
R Auto display of new email does not work on non-default account Outlook VBA and Custom Forms 0
B Outlook 2016 Auto-archive creates new folder Using Outlook 3
J Edit auto-complete list in Outlook 2016+/365? Using Outlook 0
P Auto assign shared mailbox Outlook VBA and Custom Forms 1
M Outlook 2010 Problem with OutLook 2010 32 bit, after Windows Auto Update Using Outlook 3
P [SOLVED] Auto remove [EXTERNAL] from subject Using Outlook 16
Z Add text to auto-forwarded e-mail Outlook VBA and Custom Forms 4
N Disable Auto Read Receipts sent after using Advanced Find Using Outlook 4
Q Prompt button to auto turn on Out of Office Outlook VBA and Custom Forms 3
P Auto Insert Current Date or Time into Email Subject Outlook VBA and Custom Forms 2
S Messages moved / deleted by auto-archive are not synchronized to exchange Exchange Server Administration 8
B Outlook 2010 is Auto Purging when not configured for that Using Outlook 1
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Auto Accept Meetings from the General Calendar Using Outlook 3
R auto send email when meeting closes from a shared calendar only Outlook VBA and Custom Forms 2
S auto-mapping mailboxes in outlook impacting an ost file? Exchange Server Administration 2
M Auto expand Distribution List Before Sending Email Outlook VBA and Custom Forms 1
M Auto-export mail to Excel Outlook VBA and Custom Forms 2
Ms_Cynic Auto-pasting email content in calendar appt? Using Outlook 2
R How Do I insert images in and Auto Reply Using Outlook 3
S Received mail as part of DL, need to auto-CC the same when replying Outlook VBA and Custom Forms 5
T Have Outlook 2016 suggest email address auto complete entries directly from the user's contacts list Using Outlook 10
T Have Outlook 2016 suggest email address auto complete entries directly from the user's contacts list Using Outlook 0
P Auto scroll to specific folder in Folder Pane Outlook VBA and Custom Forms 3
C Auto categorize duplicate subjects Outlook VBA and Custom Forms 11
N Auto-complete - block select emails Using Outlook 3
C Auto save outlook attachments when email is received Outlook VBA and Custom Forms 1
J HELP- Rule to auto strip prepend from external emails Using Outlook 0
S BCM Auto Backup Data and Customizations BCM (Business Contact Manager) 6
G Auto accept meeting request for non primary account Outlook VBA and Custom Forms 1
J Outlook Rules - Changing auto-submit address in multiple rules, according to rule name Outlook VBA and Custom Forms 0
E Outlook Form - Voting Responses Not Auto Processing If Form Contains Any Code Outlook VBA and Custom Forms 0
J Auto Forward - Include Attachment and change Subject depending on original sender Outlook VBA and Custom Forms 3
K Extract email address from body and auto-reply outlook Using Outlook 1
S Auto move search results to folder Outlook VBA and Custom Forms 0
E Outlook 2010 disable date auto-complete Using Outlook 2
C Auto subject,name,email,deferred Using Outlook 2
ashcosta2 Auto Reply rule based on speficied time Outlook VBA and Custom Forms 0
B Auto Preview Attachment in Inspector Reading Pane Outlook VBA and Custom Forms 1
Z Auto Forward Using Outlook 4
M can anyone recommend an alternative to DS auto-followup? Using Outlook 2
S Rules to auto redirect Using Outlook 5
B Auto Save of Attachments from Multiple Emails and forward attachments to user group Outlook VBA and Custom Forms 1

Similar threads

Top