Accessing E-mails in Outlook from Access VBA (1 Viewer)

cornerman8

New member
Local time
Today, 00:05
Joined
May 13, 2008
Messages
3
I'm pretty new to VBA. Here's my dilemma. I need to take e-mails from Outlook that have been delimited, and have them placed in a table in Access table. The goal: as new e-mails come in they are added to the table wherein queries can be ran. I've tried linking tables but I don't get the body of the e-mail which is what I'm really after. I'd appreciate any help or at least a starting point. Thanks
 

rmayer

New member
Local time
Today, 06:05
Joined
May 14, 2008
Messages
2
Sorry, the link doesn't have the info i was hoping to give.
maybe the following code is of help as a starting point, (it has to be added in outlook)


Public Sub ProcessEmail()
Dim myNS As NameSpace
Dim myInbox As MAPIFolder
Dim f1 As MAPIFolder
Dim f2 As MAPIFolder
Dim dest As MAPIFolder
Dim msg As Object
Dim ToBeDeleted() As MailItem
Dim ToBeMoved() As MailItem
Dim NumberToDelete As Integer
Dim NumberToMove As Integer
Dim idx As Integer

' Initialize variables.
NumberToDelete = 0
NumberToMove = 0
' Get a reference to the Inbox.
Set myNS = GetNamespace("MAPI")
Set myInbox = myNS.GetDefaultFolder(olFolderInbox)
' Get a reference to the destination folder.
Set dest = Nothing
For Each f1 In myNS.Folders
For Each f2 In f1.Folders
If f2.Name = "DEI_FILES" Then
Set dest = f2
End If
Next
Next

' If the destination folder wasn' t found, display
' a message and then clean up and exit.
If dest Is Nothing Then
MsgBox "The destination folder does not exist."
Set f1 = Nothing
Set f2 = Nothing
Set myNS = Nothing
Set myInbox = Nothing
Exit Sub
End If

' Loop through all messages in the Inbox.
For Each msg In myInbox.Items
' Process only mail messages.
If TypeOf msg Is MailItem Then
' If the subject contains "free", mark the
' message for deletion.
If InStr(1, msg.Subject, "Free", _
vbTextCompare) > 0 Then
NumberToDelete = NumberToDelete + 1
ReDim Preserve ToBeDeleted(NumberToDelete)
Set ToBeDeleted(NumberToDelete) = msg
' If the message is from "Linda Cooper",
' mark it to be moved.
'ElseIf msg.SenderName = "Linda Cooper" Then
ElseIf InStr(1, msg.Subject, "dei file", _
vbTextCompare) > 0 And InStr(1, msg.SenderName, "@dell.com", vbTextCompare) > 0 Then
NumberToMove = NumberToMove + 1
ReDim Preserve ToBeMoved(NumberToMove)
Set ToBeMoved(NumberToMove) = msg
End If
End If
Next
' Delete messages marked for deletion (if any).
If NumberToDelete > 0 Then
For idx = 1 To NumberToDelete
ToBeDeleted(idx).Delete
Next
End If
' Move messages marked to be moved (if any).
If NumberToMove > 0 Then
For idx = 1 To NumberToMove
ToBeMoved(idx).Move dest
Next
End If
' Clean up.
Set myNS = Nothing
Set myInbox = Nothing
Set f1 = Nothing
Set f2 = Nothing
Set dest = Nothing
Set msg = Nothing
End Sub
Private Sub Application_NewMail()
Call ProcessEmail
End Sub
 

cornerman8

New member
Local time
Today, 00:05
Joined
May 13, 2008
Messages
3
I think I may have worded my problem wrong. Currently, I've got a mailbox that recieves e-mails. The contents of these e-mails (the body of the e-mail) contains text that is in CSV format (comma separated). My goal is to take each e-mail's content and put it in a table.
 

memaxt

Registered User.
Local time
Yesterday, 21:05
Joined
Mar 12, 2013
Messages
62
This is exactly what I'm trying to achieve.. Is the above possible? Import emails that contain delimited text, add to table as and when a new emails comes in
 

Users who are viewing this thread

Top Bottom