extract outlook message body into excel?

sampson32

Active Member
Joined
Jul 16, 2003
Messages
312
Office Version
  1. 2021
Platform
  1. Windows
Another Project:

One of our engineers receives a lot of emails requesting our “Product CD”

All of the emails received are in the format below:

*********************************
…………….(Subject Line)………….

FW: ORDER For Product Information CD

…………..(Message Body)…………….

Please provide the following prospective client with the
requested information as soon as possible.

Sent To: engineering@CCengineering.com
Last Name: KINGMAN
First Name: GEORGE
Company: KINGMAN ENGINEERING
E-Mail: GEORGEKINGMAN@EMAIL.COM
Phone: 800-775-4623, Ext.
Fax: 888-775-4635
Street Address: 2004 N.E. 77th STREET
City, State, ZIP: TAMPA, FL 33619

Questions/Comments:
Replace this text with your questions/comments
************************************

Below is a macro I ran across that is supposed to extract specific text lines and put them into excel in the following format:

Last Name (A1) First Name (B1) Company (C1) Street Address (D1) City, State, ZIP (E1)

KINGMAN (A2) GEORGE (B2) KI..ENG…(C2) 2004 N.E. 77...(D2) Tampa, FL, 33612 (E2)

(Some of the above data is truncated in this example to fit in one line)

************************************

This is the code:


Option Explicit
Option Compare Text

Sub ReadInbox()
Dim appOL As Outlook.Application
Dim oSpace As Outlook.NameSpace
Dim oFolder As Outlook.MAPIFolder
Dim oItems As Outlook.Items
Dim oMail As Outlook.MailItem

Set appOL = CreateObject("Outlook.Application")
Set oSpace = appOL.GetNamespace("MAPI")
Set oFolder = oSpace.GetDefaultFolder(olFolderInbox)
Set oItems = oFolder.Items
oItems.Sort "Received", True
For Each oMail In oItems
If oMail.Subject Like "*Product Information CD*" Then
Call bodyStrip(oMail)
End If
Next
End Sub

Sub bodyStrip(msg As Outlook.MailItem)
Dim sBody As String
Dim aFields As Variant
Dim r As Range
Dim n&, iPos1&, ipos2&

aFields = Array("Last Name=", "First Name=", "Company=", "Street Address=", “City, State, ZIP=”)

Set r = [a65536].End(xlUp).Offset(1).Resize(, 4)
sBody = msg.Body

For n = 1 To 4
iPos1 = InStr(ipos2 + 1, sBody, aFields(n - 1))
If iPos1 > 0 Then
iPos1 = iPos1 + Len(aFields(n - 1))
ipos2 = InStr(iPos1 + 1, sBody, vbCrLf)
r(n) = Mid(sBody, iPos1, ipos2 - iPos1)
Else
Exit For
End If
Next
End Sub


The original code had four “afields” – I renamed them to coincide with our email row headings and added a fourth aField. (City, State, ZIP)

This code is totally beyond me ….

Does anyone know how to correct this code to work as I’ve outlined?

Any help appreciated.

Sam
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
dk wrote sort of the reverse macro ie to send an xl range as the body of an e-mail so he may be able to help you do the reverse. Send him a PM referencing this thread: dk

HTH
 
Upvote 0
Hey Sam, I'm working on this for you. But just a suggestion...if the info posted above (ie name, phone number) is not yours...you might want to blank it out. :devilish:
 
Upvote 0
What is listed is not real information…made up for example only.

Thanks…
 
Upvote 0
Okay Sam, this has not been fully tested, but I know that you will let me know what problems occur. I have lightly tested it and it does seem to operate okay. Let me know if you have any problems.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> ReadInbox()
    <SPAN style="color:#00007F">Dim</SPAN> OutlookApp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> OA_NameSpace <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> OA_Folder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> OA_MailItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>

    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet
    
    <SPAN style="color:#00007F">Dim</SPAN> Created <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>

    <SPAN style="color:#00007F">Dim</SPAN> NextRecord <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>

    <SPAN style="color:#00007F">Set</SPAN> ws = Workbooks.Add(xlWorksheet).Sheets(1)

    ws.[A1] = "Last Name"
    ws.[B1] = "First Name"
    ws.[C1] = "Company"
    ws.[D1] = "Address"
    ws.[E1] = "City, State, Zip"

    NextRecord = 2

    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> OutlookApp = GetObject(, "Outlook.Application")
    <SPAN style="color:#00007F">If</SPAN> OutlookApp <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">Set</SPAN> OutlookApp = CreateObject("Outlook.Application")
        Created = <SPAN style="color:#00007F">True</SPAN>
        <SPAN style="color:#00007F">If</SPAN> OutlookApp <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
            MsgBox "Unable to start Outlook."
            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0

    <SPAN style="color:#00007F">Set</SPAN> OA_NameSpace = OutlookApp.GetNamespace("MAPI")
    <SPAN style="color:#00007F">Set</SPAN> OA_Folder = OA_NameSpace.GetDefaultFolder(6)

    OA_Folder.Items.Sort "Received", <SPAN style="color:#00007F">True</SPAN>

    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> OA_MailItem <SPAN style="color:#00007F">In</SPAN> OA_Folder.Items
        <SPAN style="color:#00007F">If</SPAN> OA_MailItem <SPAN style="color:#00007F">Like</SPAN> "*Product Information CD*" <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#00007F">Dim</SPAN> OrderInfo <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>

            OrderInfo = GrabInfo(OA_MailItem.Body)

            <SPAN style="color:#00007F">If</SPAN> IsArray(OrderInfo) <SPAN style="color:#00007F">Then</SPAN>
                ws.Range(Cells(NextRecord, 1), Cells(NextRecord, 5)) = OrderInfo
                NextRecord = NextRecord + 1
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN>

    <SPAN style="color:#00007F">If</SPAN> Created <SPAN style="color:#00007F">Then</SPAN> OutlookApp.Quit

    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>

    <SPAN style="color:#00007F">Set</SPAN> OA_Folder = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> OA_NameSpace = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> OutlookApp = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> GrabInfo(message <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> tmpInfo(4) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>

    <SPAN style="color:#00007F">Dim</SPAN> f <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>

    <SPAN style="color:#00007F">Const</SPAN> TMPFILE <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "C:\Temp\outlook_extraction.tmp"

    <SPAN style="color:#00007F">If</SPAN> message = vbNullString <SPAN style="color:#00007F">Then</SPAN> GrabInfo = vbNullString: <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN>

    f = FreeFile

    <SPAN style="color:#00007F">Open</SPAN> TMPFILE <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Output</SPAN> <SPAN style="color:#00007F">As</SPAN> #f
        <SPAN style="color:#00007F">Write</SPAN> #f, message
    <SPAN style="color:#00007F">Close</SPAN> #f

    f = FreeFile

    <SPAN style="color:#00007F">Open</SPAN> TMPFILE <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Input</SPAN> <SPAN style="color:#00007F">As</SPAN> #f

    <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">While</SPAN> <SPAN style="color:#00007F">Not</SPAN> EOF(f)
        <SPAN style="color:#00007F">Dim</SPAN> tmpLine <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>

        Line <SPAN style="color:#00007F">Input</SPAN> #f, tmpLine

        <SPAN style="color:#00007F">If</SPAN> InStr(1, tmpLine, ":") <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> UCase(Left(tmpLine, InStr(1, tmpLine, ":") - 1))
                <SPAN style="color:#00007F">Case</SPAN> "LAST NAME": tmpInfo(0) = SplitString(tmpLine, ":")
                <SPAN style="color:#00007F">Case</SPAN> "FIRST NAME": tmpInfo(1) = SplitString(tmpLine, ":")
                <SPAN style="color:#00007F">Case</SPAN> "COMPANY": tmpInfo(2) = SplitString(tmpLine, ":")
                <SPAN style="color:#00007F">Case</SPAN> "STREET ADDRESS": tmpInfo(3) = SplitString(tmpLine, ":")
                <SPAN style="color:#00007F">Case</SPAN> "CITY, STATE, ZIP": tmpInfo(4) = SplitString(tmpLine, ":")
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Loop</SPAN>

    <SPAN style="color:#00007F">Close</SPAN> #f

    Kill TMPFILE

    GrabInfo = tmpInfo
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> SplitString(value <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, delimeter <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    SplitString = Trim(Mid(value, InStr(1, value, delimeter) + 2, Len(value) - InStr(1, value, delimeter)))
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>
</FONT>

:eek: This code will bug if the user does not have a TEMP directory located on the C drive. However most Windows based PC's have this.
 
Upvote 0
O.K. thanks ----- I’ll put it to work first thing in the morning……………I'll let you know if I run into any problems.

Many, many thanks for all the help!

Sam
 
Upvote 0
Thanks TommyGun the data transfer works great!

Just one small problem: after the data is moved to Excel - a small square box appears before the data in each column? I looked at the code you supplied but with my limited knowledge could not find anything.

Sam
 
Upvote 0
Really? Hmm....sounds like it's a line feed character. Wonder what that is doing in there? Can you send me one of the emails (without sensitive data of course), so that I can test to see if I get the same results.
 
Upvote 0
Below is the exact subject line and email body format with fictitious information
If you PM your email address - I can also email this to you if you want.

Sam


…………….(Subject Line)………….

FW: ORDER For Product Information CD


…………..(Message Body)…………….

-----Original Message-----
From: automatedresponse@ccengineering.com
[mailto:automatedresponse@castcrete.com]
Sent: Tuesday, November 18, 2003 10:06 AM
To: Tom Franks
Subject: ORDER for Product Information CD



Order Submission
http://www.ccengineering.com
-------------------------------------------------------------

Please provide the following prospective client with the
requested information as soon as possible.

Sent To: engineering@ccengineering.com
Last Name: KINGMAN
First Name: GEORGE
Company: KINGMAN ENGINEERING
E-Mail: GEORGEKINGMAN@EMAIL.COM
Phone: 800-775-4623, Ext.
Fax: 888-775-4635
Street Address: 2004 N.E. 77th STREET
City, State, ZIP: TAMPA, FL 33619

Questions/Comments:
Replace this text with your questions/comments

-------------------------------------------------------------

If you experience any difficulties with this automated form,
please contact webmaster@ccengineering.com
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top