Link to home
Start Free TrialLog in
Avatar of pwtucker
pwtuckerFlag for United States of America

asked on

Import email body to MS Access

I am trying to get text from a body of an email message into an Access Database.  The data is submitted via a web form and then emailed to a certian address.  What I want to do is import this text into an Access database without having to do type or copy and paste.  Is this possible?  

Example Email:
Firstname: John
Lastname: Doe
Comments: How can I import this data?
Avatar of Billystyx
Billystyx

can it be saved as a text file first,(from your email program) and then imported to Access?
Avatar of pwtucker

ASKER

I can do a file save as from outlook (save the entire message).
This will read the data of a subFolder called Jaffer, it will read the following:
Folder Name, Sender Name, Subject, email Received Time, Body

And it will save these data in Access, in a Table called tbl_FromOutlook with the following field names:
FolderName (text), Name (text), Subject (text), ReceivedTime (date/time), Body (memo)
Required is set to NO, Allow Zero length is set to Yes

Please make a command button on a Form, call theis butoon, command1

Private Sub Command1_Click()
On Error GoTo Err_Command1_Click

Dim olApp As Outlook.Application
Dim OlMapi As Outlook.NameSpace
Dim OlFolder As Outlook.MAPIFolder
Dim Olmail As Object
Dim OlItems As Outlook.Items
Dim rst As Recordset

Set rst = CurrentDb.OpenRecordset("tbl_FromOutlook")    ' - Access Table
Set olApp = CreateObject("Outlook.Application")         ' or Set OlApp = New Outlook.Application
Set OlMapi = olApp.GetNamespace("MAPI")
'Set OlFolder = OlMapi.GetDefaultFolder(olFolderInbox)  ' - Open the inbox

'Normal Outlook folder is called [Personal Folders], I called mine [2004-Emails],
'Then the Inbox, then the subfolder in the Unbox is called "Jaffer"

    'Set your Folder Names here
    Set OlFolder = OlMapi.Folders("2004_Emails").Folders("Inbox").Folders("Jaffer")
    Set OlItems = OlFolder.Items

For Each Olmail In OlItems
    If Olmail.UnRead = True Then
       rst.AddNew
        rst!FolderName = OlFolder
        rst!Name = Olmail.SenderName
        rst!Subject = Olmail.Subject
        rst!ReceivedTime = Olmail.ReceivedTime
        rst!Body = Olmail.Body
   
    Olmail.UnRead = False 'Mark mail as read, so that you don't read it again
       rst.Update
     End If  'UnRead
Next


Exit_Command1_Click:
    Exit Sub

Err_Command1_Click:
    MsgBox Err.Description
    Resume Exit_Command1_Click
   
End Sub
Thanks jjafferr...this sounds great.  So this function will read the contents of the body.  All I am interested in is the content of the body, which is formatted like this:
Firstname: John
Lastname: Doe
Comments: How can I import this data?

I want to get just the Firstname and insert it into the FName field in my table, get the Lastname and insert into the LName field in my table, so on and so forth.  This function probably will not do that, but I bet I could just grab the body of the email and insert it into a text file (or maybe an excel file) and then read the data from their and insert it into my table.  Does this sound doable to the experts?
This will do it too (get a text file's name value pairs into access), but cruder. You will need nothing else but the data in the text file though.

Dim name1
Dim name2
Open "C:\Text.txt" For Input As #1   ' Open file for reading.
Do While Not EOF(1)   ' Loop until end of file.
     Input #1, FirstName, LastName
  Loop

Close #1

name1= FirstName
name2= LastName
Size1 = Len(name1)
FName= Right(name1, (Size1 - 10))
Size2 = Len(name2)
LName= Right(name2, (Size2 - 9))
'if you add a message box here you'll see it works
MsgBox "firstname="& FName &",Lastname="& LName

'from here though, its up to you. Now FName would equal John and LName would equal Doe, and you can sql them into your dbase or something (didn't realy look at that). If you have more fields per email, just add a name for them on the line that starts Input #1, FirstName, LastName, etc, and then the extra bits at the bottom cutting them up and getting them ready for the database. I know its crude, but maybe its what you're looking for ...
ASKER CERTIFIED SOLUTION
Avatar of jjafferr
jjafferr
Flag of Oman image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks jjafferr...i will give it a shot this afternoon or tomorrow and let you know how it turns out.  Thanks a lot for the help and you don't know how much work this will save us if we get this to work.  Thanks again for the help.
ReCycle is the key word,
which I am doing with code too ;o)
Hi pwtucker

How did it go?
Finally got to try it.  I am getting a type mismatch on the following line:
Set rst = CurrentDb.OpenRecordset("tbl_FromOutlook")

Any suggestions???
please change the line to:
Set rst = CurrentDb.OpenRecordset("select * from tbl_FromOutlook")
Thanks jjafferr...this is working great for us.