How to read each line of an e-mail for processing in Outlook VBA

Posted on 2006-04-29
Last Modified: 2008-02-01

I have the following code to process an incoming e-mail in Outlook:

    Dim strID As String
    Dim olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    strID = oEmail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set olMail = olNS.GetItemFromID(strID)
    olMail.BodyFormat = olFormatPlain   '   If it's HTML, convert it to plain text

I can get the body of the e-mail with olMail.Body.

Can someone please provide the code that will read the body of the e-mail one line at a time and put it into a string sLine?

Question by:SAbboushi
    LVL 44

    Expert Comment

    Hello SAbboushi,

    the olMailBody is already a long string, so you need to save it to textfile first and read it line by line from there
    a sample is in this accepted answer >

    hope this helps a bit

    Author Comment

    Thanks - this is not really what I am asking -

    I wanted code that would do something like the following, but maybe more 'elegantly':

        aEmail = Split(olMail.Body, Chr(13))
        nEmailRows = UBound(aEmail)

        For nRowPtr = 0 To nEmailRows
            sRow = aEmail(nRowPtr)

           ' code to do whatever I want with sRow...

    LVL 5

    Expert Comment

    Your solution looks quite elegent to me. You have the email in a string, so any solution will have to involve splittind it into segments. I'm not absolutely sure, but I think Outlook treats CR+LF as the line break, though. And a For Each loop can be a little more elegant.

    I would do it something like the following:

        aEmail = Split(olMail.Body, vbCRLF)
        Dim sRow as String

        For Each sRow In aEmail
            ' code to do whatever I want with sRow...


    Author Comment

    Hi lostcarpark-

    I tried your code - compile error on "For Each sRow ..."

    sRow was highlighted and the error is "For Each control variable must be Variant or Object"

    I'm not sure what to do with this (I never learned what a variant/collection/object are).  Help text says:  

    A control variable is the element part of the For Each...Next statement syntax. This error has the following causes and solutions:

    A collection has a control variable that isn't a Variant or Object type.
    Make sure the element part of the For Each...Next is a Variant or Object.

    An array has a control variable that isn't a Variant.
    Make sure the element part of the For Each...Next is a Variant.

    LVL 5

    Accepted Solution


    I think in my previous post I made the mistake of declaring sRow as a String.

    I just did a quick test in Outlook 2000. If you don't declare the variable, it will treat it as a variant.

        aEmail = Split(olMail.Body, vbCRLF)
        For Each sLine In aEmail
           MsgBox sLine

    Author Comment

    Thanks -

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Suggested Solutions

    This article will show, step by step, how to integrate R code into a R Sweave document
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 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

    14 Experts available now in Live!

    Get 1:1 Help Now