Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-29
6
Medium Priority
?
347 Views
Last Modified: 2008-02-01
Hi-

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?

Thanks-
0
Comment
Question by:SAbboushi
  • 3
  • 2
6 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16571309
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 > http://www.experts-exchange.com/Q_21293733.html

hope this helps a bit
bruintje
0
 

Author Comment

by:SAbboushi
ID: 16573663
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...

    Next
0
 
LVL 5

Expert Comment

by:lostcarpark
ID: 16573797
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...

    Next
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:SAbboushi
ID: 16573868
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.

0
 
LVL 5

Accepted Solution

by:
lostcarpark earned 2000 total points
ID: 16574109
Hi,

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
    Next
0
 

Author Comment

by:SAbboushi
ID: 16574346
Thanks -
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Introduction to Processes
Screencast - Getting to Know the Pipeline

564 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