Automate Outlook

Automate Outlook, open Excel and run Macro on .xls file attachment

    I suppose this page should really be called "how to Automate Excel from Outlook", since we're really using Outlook to automate Excel.

    Oftentimes I find it extremely useful to completely automate some macro process using VBA; to make a procedure completely hands-off. I can leave Outlook running 24/7 and then use a procedure like the one below to run an Excel macro from Outlook.

    This code runs as an event, checking each incoming email for certain characteristics. If an email matches the profile, it opens the attachment (by saving it first), runs a pre-written macro, then cleans up and moves the now-processed email out of the Inbox to another folder.

    You could easily adapt this code to many other purposes, for example, set up an automated file request system in your office. Users would email you with specific words in the subject, and, in response, your code could run a macro or attach a specific file to a blank email and send it back to them. In fact, I went ahead and did just that; check out Outlook File Request System.

    First thing you will need to do is declare a new event handler by putting this line at the top of your ThisOutlookSession module.

Private WithEvents Items As Outlook.Items

    ThisOutlookSession is a built-in class module, so I usually put all my event/class code here to avoid confusion.

    Then, in the Application_Startup() event, place the following code to instantiate the Items event handler when Outlook starts.

Dim objNS As Outlook.NameSpace
Dim X As Integer

Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

    If you don't already have a Startup event, simply add Private Sub Application_Startup() to the top of the above code, and End Sub to the bottom. This will tell Outlook that there will be some events associated with the Items collection in your default Inbox.

    Once you paste in the code above, you will be able to choose the Items class from the dropdown boxes at the top of the VBE code window. The dropdown box on the right will show you the possible events you can monitor from the Items collection.

Outlook VBE

    Finally, here is where the real work gets done. The VBA code monitors the default Inbox for any new items added. If they are mail messages, it checks the sender, subject and number of attachments. We have already pre-determined where the email will come from, the subject line and how many attachments; if you decide to use this code in your office, you'll need to agree on a format and subject with the sender beforehand. The code will open the attached workbook, run a pre-written macro on it, then move the email to our storage folder, all without any user input needed. In this example, the macro is in the personal.xls workbook.

Private Sub Items_ItemAdd(ByVal Item As Object)
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")

If TypeOf Item Is Outlook.MailItem Then
  Dim Msg As Outlook.MailItem
  Set Msg = Item
           
  If (Msg.SenderName = "My Favorite Sender") And _
 (Msg.Subject = "Email I Need to Open") And _
 (Msg.Attachments.Count = 1) Then
       
    ' open wkbk and run import macro
   Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim XLApp As Object ' Excel.Application
   Dim XlWK As Object ' Excel.Workbook
   Dim Att As String
       
    Const attPath As String = "C:\"
       
    Set olDestFldr = objNS.Folders("Mailbox - My Personal PST Box").Folders("Inbox").Folders("Messages")
       
    ' New Excel.Application
   Set XLApp = CreateObject("Excel.Application")
       
    ' save attachment
   Set myAttachments = Item.Attachments
    Att = myAttachments.Item(1).DisplayName
    myAttachments.Item(1).SaveAsFile attPath & Att
       
    ' open personal.xls where macro is stored,
   ' just in case it doesn't open on its own
   On Error Resume Next
    XLApp.Workbooks.Open _
 ("C:\Documents and Settings\username\Application Data\Microsoft\Excel\XLSTART\PERSONAL.XLS")
    On Error GoTo 0
       
    ' open workbook and run macro
   XLApp.Workbooks.Open (attPath & Att)
       
    XLApp.Run ("PERSONAL.XLS!MacroName")
    XLApp.Workbooks.Close
    Kill attPath & Att
    XLApp.Quit
       
    ' mark as read and move to msgs folder
   Msg.UnRead = False
    Msg.Move olDestFldr
  End If
End If
End Sub

Site last updated March 17, 2010 @ 10:12 pm; This content last updated February 6, 2010 @ 6:55 am