'Option Explicit
Option Compare Text
Public StopTime
Public dTime As Date
Sub ReadInbox()
Dim aEmails()
ReDim aEmails(0)
If StopTime = "" Or StopTime = False Then
Set appOL = CreateObject("Outlook.Application")
Set oSpace = appOL.GetNamespace("MAPI")
Set oFolder = oSpace.GetDefaultFolder(olFolderInbox)
Set oItems = oFolder.Items
oItems.Sort "Received", True
'This defines the size of the array
Sheets("Emaillist").Activate
lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
ReDim aEmails(LBound(aEmails) To lastrow - 1, 0 To 1)
'this populates the array
For x = 1 To lastrow
aa = x - 1
aEmails(aa, cName) = UCase(Cells(x, xName))
aEmails(aa, cFoldr) = UCase(Cells(x, xFoldr))
Next x
On Error Resume Next
'This says for every mail item call this other routine
For Each oMail In oItems
If oMail.Subject Like "*" Then
Call bodyFind(oMail, aEmails)
End If
Next
'This makes the macro run every 30 seconds
Application.OnTime Now + TimeValue("00:00:30"), "ReadInbox"
'Application.OnTime Now + TimeValue("00:0:30"), "Savefile"
Else
StopTime = ""
End If
End Sub
Sub bodyFind(msg As Outlook.MailItem, aEmails)
Dim data1(), data2(), data3()
Dim RowCount As Long
sToName = msg.To
sFromName = msg.SenderName
sSubject = msg.Subject
sBody = msg.Body
sCC = msg.cc
sEmail = sToName & " " & sFromName & " " & sCC & " " & sFromName & " " & sSubject & " " & sBody
sEmail = UCase(sEmail)
For aa = LBound(aEmails) To UBound(aEmails)
On Error Resume Next
'This does a find function on sEmail when it finds it is supposed to
'move the email to the folder name from the array
pos1 = Application.WorksheetFunction.Find(aEmails(aa, cName), sEmail)
If Err <> 0 Then
Err = 0
Else
'here is where it identifies which folder to put the email in
Dim Myfolder As Outlook.MAPIFolder
Set oFolder = Application.GetNamespace("MAPI")
Set Myfolder = oFolder.Folders(aEmails(aa, cFoldr))
'this moves the email (or it did until we went from 250 rules/folders to 650
oMail.Move Myfolder
Sheets("Report").Select
Do Until ActiveCell = ""
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell = sFromName
ActiveCell.Offset(0, 1) = sSubject
ActiveCell.Offset(0, 2) = aEmails(aa, cName)
ActiveCell.Offset(0, 3) = aEmails(aa, cFoldr)
ActiveCell.Offset(0, 4) = Now
ActiveCell.Select
Set oFolder = Empty
Set Myfolder = Nothing
GoTo Endloop:
End If
'On Error GoTo 0
Set oFolder = Empty
Set Myfolder = Nothing
Next aa
Endloop:
End Sub
Sub EndTimes()
StopTime = True
End Sub
Here is all my code
Bookmarks