Need help to trigger outlook macro automatically when new email comes in Specific folder

Hi Experts ,

I have a  Independent folder in Outlook named "Test" .
I have macro whose work is to get the attachment from "Test" folder emails and save at"EmailAttachments" folder in "MyDocuments"

So the program is working when I manually run the macro , So I need your help to set some program which can run this macro automatically .
The program should run when new email comes in "Test" folder only otherwise if it will check inbox and all outlook then it will be performance issue.

I am using below code in "ThisOutlookSession" but it is checking all incoming emails in outlook , just need to check "Test" folder only.

Private Sub Application_NewMail()
    Call SaveAttachments
End Sub 

Open in new window



Also "Run as Script" is not available.


Appreciate any help in this regard.
Afzal KhanSoftware ProfessionalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shimshey RosenbergSysAdminCommented:
How are the emails getting into the test folder? Is it with a rule?
You can utilize the same rule to run a script.

See this link;
https://www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/
0
Afzal KhanSoftware ProfessionalAuthor Commented:
Shimshey the emails are coming through Outlook rule , Unfortunately "Run as Script" is not available in outlook due to security reasons.
0
Shimshey RosenbergSysAdminCommented:
What are you looking help with? running it only on a specific folder? or running it automatically?
0
Afzal KhanSoftware ProfessionalAuthor Commented:
I need help to run the macro automatically when new email comes only to that folder"Test" , As of now I am manually running it.
0
Afzal KhanSoftware ProfessionalAuthor Commented:
I solved it using Item_Add


Public WithEvents myOlItems As Outlook.Items
 
 Public Sub Initialize_handler()
 Set olApp = New Outlook.Application
 Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Parent.Folders("Test").Items
 
End Sub
  
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
 
Dim myOlMItem As Outlook.MailItem
Call SaveLatestAttachment
MsgBox "Task Completed Successfully.", vbInformation
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.