ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Extracting Data from MS Outlook Mail Messages (https://www.excelbanter.com/excel-programming/278554-extracting-data-ms-outlook-mail-messages.html)

Steve Wright

Extracting Data from MS Outlook Mail Messages
 
Hi all,
I have a problem.

I am recieving a number of email messages from which I need to extract the
contents of the mail message body and import it into Excel. The data comes
from HTML forms that are Emailed to me.

The body of the Mail Message contains data in the following format:

jobno=100325
contract=RS-330
jobtype=Asphalt
completedate=20/03/2003

What I want to do is extract the data from the mail message and insert it in
to Excel.
The bit before the = should be the column name
Anyone know where I should start.

TIA
Steve



zantor[_25_]

Extracting Data from MS Outlook Mail Messages
 
Hi Steve,

Do the emails all have the same subject lines / come from the same
sender?

Does the body contain only the format you specified and nothing more?



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/


keepITcool

Extracting Data from MS Outlook Mail Messages
 

Following would work for PLAINTEXT messages. For HTML you'd need to
strip any tags found..

note the routine to loop/select messages from the inbox is rudimentary
and for illustration only.

You MUST set a reference to the Outlook Object Library.
Hope it gets you on your way :)



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 "*BodyTest*" 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("jobno=", "contract=", "jobtype=", "completedate=")

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




keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"Steve Wright" wrote:

Hi all,
I have a problem.

I am recieving a number of email messages from which I need to extract
the contents of the mail message body and import it into Excel. The
data comes from HTML forms that are Emailed to me.

The body of the Mail Message contains data in the following format:

jobno=100325
contract=RS-330
jobtype=Asphalt
completedate=20/03/2003

What I want to do is extract the data from the mail message and insert
it in to Excel.
The bit before the = should be the column name
Anyone know where I should start.

TIA
Steve




Steve Wright

Extracting Data from MS Outlook Mail Messages
 
Yes the subject lines are all the same but the message may come from four or
five different people.

The body contains only the specified format


Steve
"zantor" wrote in message
...
Hi Steve,

Do the emails all have the same subject lines / come from the same
sender?

Does the body contain only the format you specified and nothing more?



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/




Steve Wright

Extracting Data from MS Outlook Mail Messages
 
Works great thanks!!

Next question!

This works with the Inbox. I would like to make it work with a folder
called "RAMM" as I have a rule in outlook that transfers those messages
there.

Also I would like to "move" the message to another mailbox that I have
access to.

Any help appreciated

Steve


"keepitcool" wrote in message
...

Following would work for PLAINTEXT messages. For HTML you'd need to
strip any tags found..

note the routine to loop/select messages from the inbox is rudimentary
and for illustration only.

You MUST set a reference to the Outlook Object Library.
Hope it gets you on your way :)



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 "*BodyTest*" 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("jobno=", "contract=", "jobtype=", "completedate=")

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




keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"Steve Wright" wrote:

Hi all,
I have a problem.

I am recieving a number of email messages from which I need to extract
the contents of the mail message body and import it into Excel. The
data comes from HTML forms that are Emailed to me.

The body of the Mail Message contains data in the following format:

jobno=100325
contract=RS-330
jobtype=Asphalt
completedate=20/03/2003

What I want to do is extract the data from the mail message and insert
it in to Excel.
The bit before the = should be the column name
Anyone know where I should start.

TIA
Steve






keepITcool

Extracting Data from MS Outlook Mail Messages
 
sorry for slow response


read www.rondebruin.nl for plenty of info on working with outlook.



keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"Steve Wright" wrote:

Works great thanks!!

Next question!

This works with the Inbox. I would like to make it work with a folder
called "RAMM" as I have a rule in outlook that transfers those messages
there.

Also I would like to "move" the message to another mailbox that I have
access to.

Any help appreciated

Steve


"keepitcool" wrote in message
...

Following would work for PLAINTEXT messages. For HTML you'd need to
strip any tags found..

note the routine to loop/select messages from the inbox is rudimentary
and for illustration only.

You MUST set a reference to the Outlook Object Library.
Hope it gets you on your way :)



All times are GMT +1. The time now is 11:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com