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

Afzal Khan
Afzal Khan used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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/
Afzal KhanSoftware Professional

Author

Commented:
Shimshey the emails are coming through Outlook rule , Unfortunately "Run as Script" is not available in outlook due to security reasons.
What are you looking help with? running it only on a specific folder? or running it automatically?
Afzal KhanSoftware Professional

Author

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.
Software Professional
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial