Option Explicit
Const DBPath = "{path to db back end}"
Const MailboxToScan = "{my email address}"
Private Sub Command0_Click()
Export_Responses_to_Access
End Sub
Private Function GetFolder(strFolderPath As String, ByRef Mailbox As Outlook.MAPIFolder) As MAPIFolder
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objFolder = Mailbox.Folders.Item(arrFolders(0))
If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
Set GetFolder = objFolder
Set colFolders = Nothing
End Function
Public Sub Export_Responses_to_Access()
Dim Db As Database
Dim ItemsToProcess As Outlook.Items
Dim ldSentDate As Date
Dim liInitialCount As Integer
Dim liRecordCount As Integer
Dim objOL As Object
Dim oInbox As Outlook.MAPIFolder
Dim oProcessed As Outlook.MAPIFolder
Dim Rst As DAO.Recordset
Dim strSubject As String
DoCmd.SetWarnings False
Set objOL = CreateObject("Outlook.Application")
Set oInbox = objOL.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set oProcessed = oInbox.Folders("Service Bulletins")
Set Db = CurrentDb
Set Rst = Db.OpenRecordset("SELECT Subject FROM tblEmailsSent;")
If Not Rst.EOF Then
Rst.MoveLast
liInitialCount = Rst.RecordCount
Rst.MoveFirst
Do While Not Rst.EOF
strSubject = Rst!Subject
Call Find_Responses(strSubject, Db, oInbox, oProcessed)
strSubject = ""
Rst.MoveNext
Loop
End If
Set objOL = Nothing
Set oInbox = Nothing
Set oProcessed = Nothing
Set ItemsToProcess = Nothing
Db.Close
Set Db = Nothing
Set Rst = Nothing
DoCmd.SetWarnings True
End Sub
Sub Find_Responses(ByVal strSubject As String, _
Db As DAO.Database, _
oInbox As Outlook.MAPIFolder, _
oProcessed As Outlook.MAPIFolder)
Dim ldRespondedDate As Date
Dim ldSentDate As Date
Dim liCount As Integer
Dim Mailbox As Outlook.MAPIFolder
Dim objMail As Outlook.MailItem
Dim objNamespace As Outlook.NameSpace
Dim objOutlook As Outlook.Application
Dim objRecipient As Outlook.Recipient
Dim propertyAccessor As Outlook.propertyAccessor
Dim Rst2 As DAO.Recordset
Dim strBody As String
Dim strRecipient As String
Dim strResponse As String
Set Rst2 = Db.OpenRecordset("SELECT * FROM tblEmailsReceived;") ' Used for appending new records
oInbox.Items.Sort "ReceivedTime", False
liCount = 0
For Each objMail In oInbox.Items
'On Error Resume Next
Set propertyAccessor = objMail.propertyAccessor
'Look for all replies received that relate to the original email, based on Subject
If objMail.Subject Like ("RE: " & strSubject & "*") Then
ldSentDate = objMail.SentOn
strRecipient = objMail.SenderName
strResponse = Clean_Response(objMail.Body, strRecipient)
ldRespondedDate = objMail.SentOn
' Store required data in Access
With Rst2
.AddNew
!Subject = strSubject
!Response = strResponse
!Respondent = strRecipient
!ResponseDate = ldRespondedDate
.Update
End With
objMail.UnRead = False
objMail.Move oProcessed
liCount = liCount + 1
End If
DoEvents
Next
Exit_Sub:
Set Rst2 = Nothing
Exit Sub
Err_Handler:
MsgBox Err.Number & Chr(13) & Err.Description, vbExclamation
Resume Exit_Sub
Resume
End Sub
Public Function Clean_Response(ByVal strEnteredString As String, ByVal strRecipient As String) As String
Dim n As Integer
Dim liPosition As Integer
For n = 1 To 31
strEnteredString = Replace(strEnteredString, Chr(n), "")
Next n
liPosition = InStr(strEnteredString, strRecipient)
If InStr(strEnteredString, "From:") < liPosition Then
liPosition = InStr(strEnteredString, "From: ")
End If
strEnteredString = Left(strEnteredString, liPosition - 1)
Clean_Response = strEnteredString
End Function