0
votes

I'm trying to set up an automated process for when I receive a email in Outlook containing "subject" it automatically fires off a macro to open the attached .xls file in Excel and run a macro I have set up in Excel. How can this be accomplished?

Can provide more information if necessary - thanks!

2
Does the attachment as macro? or you are just trying to open the attachment then call second excel file.?0m3r

2 Answers

2
votes

You would need to write the vba code in Outlook first of all. This site could help you get started. Then make it check if the attachment is a file type excel can open, and if that is the case let it open excel. Running another macro over the excel file would require the vba code of excel also opening the workbook that contains the macro you wrote, so you also need a way in which the outlook macro can insert the filename into the excel macro to identify on which workbook the excel macro needs to be ran.

Edit: Perhaps there is also a way to make the excel workbook you opened import the macro from a place you stored it on your harddisk, that would save you opening another excel file, and referencing to the file you want it to be run on. You can then just refer to "ActiveWorksheet"

1
votes

There are two possible ways implementing your task.

The first one is to create an Outlook rule an assign a VBA macro for running when the rule triggers. The VBA macro sub should look like the following one:

Public Sub Test(mail as MailItem)
   ' so something here
End Sub

The second way is to handle the NewMailEx event of the Application class. In that case there is no need to create a rule in Outlook. Your code will be triggered by the event. Here is what MSDN states:

This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item.

The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.

In the code you will need to automate Excel. The Run method of the Application class can be used to run the VBA macro programmatically.

Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.