Importing and Parsing Email Text in Excel: interesting problem

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
Thinking through a problem here and thought I would elicit thoughts from the board.

I have a few thousand emails saved as text files. [For the sake of the problem, they are all saved in .txt format]. The issue is that the emails are often chains where the recepient/sender/dates/subject can change in the course of the chain. So one Thread might contain two separate emails:

From: Sender1
To: Recepient1; Recepient2
CC: Recepient3; Recepient4
Subject: Email1
Date: Date1

Text of Email1 here

From: Recepient1
To: Sender1
CC: Recipient3
Subject: Email2
Date: Date2

Text of Email2 here

I have a macro to import all the texts into a spreadsheet. What I am thinking through is how to parse the email into the various pieces of the email chain.

Since the format for the sender/recepient/subject/date is always the same, it seems that some sort of loop would work such as:

For each text:
Find "From:" and return value then
Find "To:" and return value then . . .

Find Next From and if none go to next document.

I am happy to paste the macro for importing text if it will help anyone else think through this with me.

As always, appreciate the help.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
jscranton,


If your raw data was in Sheet1, column A, we could do something like this:


Excel Workbook
ABCDEF
1From: Sender1SenderReceipientSubjectDate
2To: Recepient1; Recepient2Sender1Recepient1, Recepient2, Recepient3, Recepient4Email1Date1
3CC: Recepient3; Recepient4Sender1Sender1, Recipient3Email2Date2
4Subject: Email1
5Date: Date1
6
7Text of Email1 here
8
9From: Recepient1
10To: Sender1
11CC: Recipient3
12Subject: Email2
13Date: Date2
14
15Text of Email2 here
16
Sheet1




We would like to see more raw data, and also the actual results area.


What version of Excel are you using?

You will generally get much more help (and faster) in this forum if you can post your small samples (sensitive data scrubbed/removed/changed) (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:

If you are not able to give us screenshots, see below in my Signature block: You can upload your workbook to Box Net, mark the workbook for sharing, and provide us with a link to your workbook.
 
Upvote 0
jscranton,


Can we also see your macro code?


If posting VBA code, please use Code Tags, see below in my Signature block: If posting VBA code, please use Code Tags - like this
 
Upvote 0
THanks for the help and forum suggestions. Using Excel 2007.

Let me take a step back as I realize the code to import texts isn't working.

So, I will start from the beginning. I have a few thousand emails that I have extracted the text from so that--for the most part--they look like this:

_________________________________________________
Sample Email

From: John Doe
Sent: Tuesday, November 05, 2011 8:46 AM
To: Jane Deer
CC: Steve Boss
Subject: RE:
Email Text.

From: Jane Deer
Sent: Tuesday, November 04, 2011 8:45 AM
To: John Doe
Subject: RE: Sensitive Data With Comments
Email Text

From: John Doe
Sent: Tuesday, November 03, 2011 8:44 AM
To: Jane Deer
Subject: RE: Sensitive Data
Email text.

From: Jane Deer
Sent: Tuesday, November 01, 2011 8:28 AM
To: John Doe
CC: Mark Coworker
Subject: FW: Sensitive Data
_______________________________________________


I would like to create code that would do import the text into excel so that it would appear like this:

Sheet1
Excel Workbook
ABCDEF
1Sample EmailFromToCCSentSubject
21John DoeJane DeerSteve BossTuesday, November 05, 2011 8:46 AMRE:
32Jane DeerJohn DoeTuesday, November 04, 2011 8:45 AMRE: Sensitive Data With Comments
43John DoeJane DeerTuesday, November 03, 2011 8:45 AMRE: Sensitive Data
54Jane DeerJohn DoeMark CoworkerTuesday, November 02, 2011 8:45 AMFW: Sensitive Data
6Next Sample
...

Excel 2007

So, there would be two loops (one nested). The first would cycle through all the saved emails in a given file and import the text. The second would cycle through the extracted text in a given sample email and parse it as I have shown above.

Does that help?
 
Upvote 0
jscranton,

I would like to create code that would do import the text into excel so that it would appear like this:

Sorry, I have not tried to read all the text files in a folder, and manipulate the text file data directly into the six fields per your screenshot.

Click on the Post Reply button, and just enter the word BUMP, and then click on the Post Reply button, and someone else will assist you.
 
Last edited:
Upvote 0
Thanks for your help. Here is where I stand at the momen. I haven't checked this code for errors but logically this should work:

Sub txtSearch()

Dim fs As FileSearch
Dim i As Integer
Dim wbk As Workbook

Set fs = Application.FileSearch

With fs
.LookIn = ThisWorkbook.Path
.Filename = "*.txt"

For i = 1 To .Execute()

Set wbk = Workbooks.Open(.FoundFiles(i))
''//RUN Other loop HERE

Set wbk = wbk.Close(SaveChanges:=True)

Next i

End With

End Sub

Sorry if this isn't the proper format to post code. Appreciate any help.
 
Upvote 0

Forum statistics

Threads
1,216,774
Messages
6,132,649
Members
449,740
Latest member
Stevejhonsy

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