PDA

View Full Version : Macro to run for all incoming email as it arrives



newboy123
05-21-2019, 03:42 AM
Hi All,

I'm brand new to VBA, i'm trying to find a way of running a macro when an email arrives. I cant find any rule actions which allow you to run scripts (which i believe was removed in later versions for security reasons).

How would I have a macro run every time an email arrives? I understand Outlook will always need to be open for this to even be a possibility but i'm not sure what needs to be added to the test macro below so it runs every time a new email comes in.

Sub SayHello()
MsgBox "Hello World!"
End Sub

thanks.

gmayor
05-21-2019, 06:05 AM
You are going to need the scripts, which can be restored to rules - https://www.extendoffice.com/documents/outlook/4640-outlook-rule-run-a-script-missing.html
Then what you want is simple

Sub HelloWorld(olItem As Outlook.MailItem) With olItem
MsgBox "Hello World!"
End With
lbl_Exit:
Exit Sub
End Sub

The macro then runs when the message arrives in the inbox.

newboy123
05-23-2019, 12:22 PM
You are going to need the scripts, which can be restored to rules - https://www.extendoffice.com/documents/outlook/4640-outlook-rule-run-a-script-missing.html
Then what you want is simple

Sub HelloWorld(olItem As Outlook.MailItem) With olItem
MsgBox "Hello World!"
End With
lbl_Exit:
Exit Sub
End Sub

The macro then runs when the message arrives in the inbox.

thats perfect!!!! Thanks so much

newboy123
05-23-2019, 12:22 PM
One related question, would any vb macro go inside the with, end with for it to run when an email comes in?