Private WithEvents insp As Outlook.Inspectors
Private WithEvents Items As Outlook.Items
Public oSORT As Dictionary
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim sPATH As String
Set oSORT = CreateObject("Scripting.Dictionary")
sPATH = "C:WorkDDEToolsOutlookFilters.csv"
Call SortDict(oSORT, sPATH)
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub SortDict(ByRef oDICT As Dictionary, sPATH As String)
Dim sARR() As String
Open sPATH For Input As #1
Do Until EOF(1)
Line Input #1, sVAR
sARR() = Split(sVAR, ",")
oDICT(sARR(0)) = sARR(1)
Loop
Close #1
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim oNAMESPACE As Outlook.NameSpace
Dim oFOLDER As Outlook.Folder
Dim sVAR As String, sTO As String, sFROM As String
Dim sARR() As String
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim i As Integer, k As Integer
If TypeName(Item) = "MailItem" Then
Set Msg = Item
Set oNAMESPACE = Application.GetNamespace("MAPI")
Set oFOLDER = oNAMESPACE.GetDefaultFolder(olFolderInbox)
sFROM = Msg.SenderEmailAddress
If oSORT.Exists(sFROM) Then
If InStr(1, oSORT(sFROM), ";", vbTextCompare) Then
sARR() = Split(oSORT(sFROM), ";")
For i = LBound(sARR) To UBound(sARR)
Set oFOLDER = oFOLDER.Folders(sARR(i))
Next i
Else
Set oFOLDER = oFOLDER.Folders(oSORT(sFROM))
End If
Msg.Move oFOLDER
Set oFOLDER = Nothing
Else
Set recips = Msg.Recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
sTO = pa.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
If oSORT.Exists(sTO) Then
If InStr(1, oSORT(sTO), ";", vbTextCompare) Then
sARR() = Split(oSORT(sTO), ";")
For i = LBound(sARR) To UBound(sARR)
Set oFOLDER = oFOLDER.Folders(sARR(i))
Next i
Else
Set oFOLDER = oFOLDER.Folders(oSORT(sTO))
End If
Msg.Move oFOLDER
Set oFOLDER = Nothing
Exit For
End If
Next
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
|