Outlook parse incoming mail VBA

Posted on 2011-10-27
Last Modified: 2012-05-12
Can someone tell me how to code VBA in Outlook.
I am accustomed to VBA in Excel, I do custom ribbons and addins etc, userforms, but have no idea how to put VBA code into Outlook.

I want Outlook to parse the body, subject, to and from fields of every incoming email and save the text to a SQL database. I know how to send commands in SQL language, that bit is no problem.
Then I want Outlook to save the email message in a Windows folder (just like when you go Ctrl + C on an email and then Ctrl + V in a Windows folder and it pastes the email message as a file).
If you need more info let me know.
Outlook versions are all either 2007 or 2010.
Question by:p-plater
    LVL 5

    Expert Comment


    even I would like know about more like this requirement in outlook, experts please help us.

    LVL 26

    Accepted Solution

    Hi, p-plater.

    First some warnings...
    (1) My VBA skills are primarily in Excel. I'm at little further into Outlook than you are, but not a lot. If a true Outlook expert comes along - go with him/her!
    (2) There's some noise (including from Microsoft) about events not being fired when a "lot" of emails are received. I've not seen any definition of "lot".
    (3) If you're an Exchange user then all bets are off.
    (4) Outlook Rules and anti-virus/spam operate asynchronously, and so may interfere with the macro.
    (5) You may get errors for "funny" items (e.g. Meeting requests, Read Receipts).

    Still here?! OK, open the Visual Basic Editor. You should see "Microsoft Outlook Objects" in the Project Explorer pane. Expand that to display ThisOutlookSession. Double-click on that and enter the following code...
    Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim varEntryIDs
    Dim objItem As MailItem
    Dim i As Integer, j As Integer
    Dim xRecipients As String
    varEntryIDs = Split(EntryIDCollection, ",")
    For i = 0 To UBound(varEntryIDs)
        Set objItem = Application.Session.GetItemFromID(varEntryIDs(i))
        For j = 1 To objItem.Recipients.Count
            xRecipients = xRecipients & objItem.Recipients(j).Address & ";"
        Debug.Print "NewMailEx - " & varEntryIDs(i) & " :: " & objItem.subject & " :: " & xRecipients & " :: " & objItem.SenderEmailAddress & " :: " & Mid(objItem.body, 1, 99)
        objItem.SaveAs "D:\" & varEntryIDs(i) & ".msg", olMSG
    End Sub

    Open in new window

    For each email received, this debug.prints so sample fields. It also saves the email to the D drive named with the email's EntryID.



    Author Closing Comment

    Thanks heaps
    LVL 26

    Expert Comment

    Thanks, p-plater!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    My experience with Windows 10 over a one year period and suggestions for smooth operation
    Email signatures have numerous marketing benefits. Here are 8 top reasons to turn your email signature into a marketing channel.
    The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now