Welcome to Sol 3 Sign in | Join | Help
CS Search | Live Search Search

Sol 3

Home of Barrows Software Solutions, LLC

Keith Barrows - StarPilot

Subjects range from Personal to Technical.

Handling Outlook Mail in VBA for Outlook

I am on way too many email lists. Between my IT career, space related sites, science R&D, Marine Corps, SCA, skydiving or what have you. I got tired of making rules for each list and finally decided to use programming to skin this cat. It took some time looking up all the Outlook objects but this is what I finally came up with in VBA Most of this could convert to .NET pretty easily if the need was there.

   ' ENTRY POINT #1:  Called by a toolbar action (button)                                 '

   '---------------------------------------------------------------------------------------'

   Public Sub RunSorter()

        On Error GoTo errHandler

        Dim target As String

        Dim varEntryIDs

        Dim objItem As Object

        Dim i As Integer

        Dim message As String

 

        Dim myFolder As Outlook.MAPIFolder

        Dim x As Integer

 

        myFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

 

        For x = 1 To myFolder.Items.Count

            If (TypeName(myFolder.Items(x)) = "MailItem") Then

               target = sortIncoming(myFolder.Items(x))

               If Len(target) > 0 Then

                    message = message & vbCrLf & target

               End If

            End If

        Next x

        message = message & vbCrLf & "--[ DONE ]------------------------------------"

        Call MsgBox(message, vbOKOnly, "Messages moved...")

 

exitHandler:

        Exit Sub

 

errHandler:

        Resume Next

 

   End Sub

 

   '---------------------------------------------------------------------------------------'

   ' ENTRY POINT #2:  Automatically runs when new mail is dropped in                       '

   '---------------------------------------------------------------------------------------'

   Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

        On Error GoTo errHandler

        Dim target As String

        Dim varEntryIDs

        Dim objItem As Object

        Dim i As Integer

 

        varEntryIDs = Split(EntryIDCollection, ",")

        For i = 0 To UBound(varEntryIDs)

            objItem = Application.Session.GetItemFromID(varEntryIDs(i))

            Debug.Print("NewMailEx " & objItem.Subject)

            If (TypeName(objItem) = "MailItem") Then

               target = sortIncoming(objItem)

            End If

        Next

 

exitHandler:

        Exit Sub

 

errHandler:

        Resume Next

 

   End Sub

 

   '---------------------------------------------------------------------------------------'

   '                                                                                       '

   '---------------------------------------------------------------------------------------'

   Private Function sortIncoming(ByVal mail As MailItem) As String

        On Error GoTo errHandler

        Dim targetPst As String

        Dim targetFolders

        Dim sourceDomain As String

        Dim sourceList As String

        Dim sourceAddress As String

        Dim message As String

        Dim myExplorers As Outlook.Explorers

        Dim pstFolder As MAPIFolder

        Dim targetFolder As MAPIFolder

        Dim pstFolderFound As Boolean

        Dim targetFolderFound As Boolean

        Dim isGoodAddress As Boolean

        Dim isValidMove As Boolean

        Dim i As Integer

        Dim j As Integer

        Dim k As Integer

        Dim myArray() As String

        Dim myXml As String

 

        Call MakeArray(myArray)

 

        'sourceAddress = GetAddress(mail)

        For k = 1 To mail.Recipients.Count

            sourceAddress = mail.Recipients.Item(k).Address

            message = ""

 

            If (InStr(1, sourceAddress, "@") > 0) Then

               sourceDomain = LCase(Mid(sourceAddress, InStr(1, sourceAddress, "@") + 1))

               sourceList = LCase(Left(sourceAddress, InStr(1, sourceAddress, "@") - 1))

               pstFolderFound = False

               targetFolderFound = False

 

               myExplorers = mail.Application.Explorers

               isGoodAddress = False

               isValidMove = False

 

               ' Handle the Advice series of lists...

               If (InStr(1, sourceDomain, "advice.com") > 0) Then

                    If (sourceDomain = "aspadvice.com") Then

                        sourceList = "AspAdvice-" & sourceList

                    ElseIf (sourceDomain = "sqladvice.com") Then

                        sourceList = "SqlAdvice-" & sourceList

                    ElseIf (sourceDomain = "xmladvice.com") Then

                        sourceList = "XmlAdvice-" & sourceList

                    End If

                    isGoodAddress = True

                    targetPst = "Tech Communities"

                    targetFolders = Split(sourceList, "-")

               End If

 

               If (InStr(1, sourceDomain, "yahoogroups.com") > 0) Then

                    For x = 0 To UBound(myArray, 2)

                        If UCase(sourceList) = UCase(myArray(1, x)) Then

                           isGoodAddress = True

                           targetFolders = Split(myArray(2, x), "-")

                           targetPst = myArray(0, x)

                           Exit For

                        End If

                    Next x

               End If

 

               If isGoodAddress Then

                    ' These are the top level (PST) folders...

                    For i = 1 To myExplorers.Session.Folders.Count

                        ' Are we in the right folder yet?

                        If (UCase(myExplorers.Session.Folders.Item(i)) = UCase(targetPst)) Then

                           pstFolder = myExplorers.Session.Folders.Item(i)

                           targetFolder = pstFolder

 

                           ' Let's create the new folder if it does not exist (recursive)...

                           For j = 0 To UBound(targetFolders)

                                targetFolder = GetMakeFolder((targetFolders(j)), targetFolder)

                           Next j

 

                           ' Now, let's move the mail there and make sure it's marked as unread...

                           Call mail.Move(targetFolder)

                           mail.UnRead = True

                           isValidMove = True

                           message = mail.Subject

                           Exit For

                        End If

                    Next i

 

                    targetFolderFound = False

                    pstFolderFound = False

               End If

            End If

 

            If isValidMove Then

               Exit For

            End If

        Next k

 

exitHandler:

        sortIncoming = message

        Exit Function

 

errHandler:

        Resume Next

 

   End Function

 

 

   '---------------------------------------------------------------------------------------'

   '                                                                                       '

   '---------------------------------------------------------------------------------------'

   Private Function GetMakeFolder(ByVal targetName As String, ByVal targetFolder As MAPIFolder) As MAPIFolder

        On Error GoTo errHandler

        Dim targetFolderFound As Boolean

        Dim newTargetFolder As MAPIFolder

 

        For i = 1 To targetFolder.Folders.Count

            If (targetFolder.Folders(i) = targetName) Then

               targetFolderFound = True

               newTargetFolder = targetFolder.Folders(i)

               Exit For

            End If

        Next i

 

        If Not targetFolderFound Then

            newTargetFolder = targetFolder.Folders.Add(targetName)

        End If

 

exitHandler:

        GetMakeFolder = newTargetFolder

        Exit Function

 

errHandler:

        Resume Next

 

   End Function

 

   '---------------------------------------------------'

   ' These are the Yahoo and/or Google lists           '

   '---------------------------------------------------'

   Private Sub MakeArray(ByRef myArray() As String)

        Dim i As Integer

 

        i = 0

        ReDim myArray(2, i)

 

        ' Most lists taken out for brevity...

        i = AddToArray(myArray, "[Sharpen the Saw]", "LinkedinUSMC", "Social-USMC", i)

        i = AddToArray(myArray, "The Terran Institute", "space-elevator", "Space-Elevator", i)

   End Sub

 

   Private Function AddToArray(ByRef myArray() As String, ByVal pstFolderName As String, ByVal emailName As String, ByVal targetFolders As String, ByVal index As Integer) As Integer

        ReDim Preserve myArray(2, index)

        myArray(0, index) = pstFolderName

        myArray(1, index) = emailName

        myArray(2, index) = targetFolders

        AddToArray = index + 1

   End Function

 

Published Friday, October 12, 2007 11:55 AM by Keith Barrows
Filed under: , ,

Comments

 

Keith Barrows - Aggregated said:

I am on way too many email lists. Between my IT career, space related sites, science R&D, Marine

October 12, 2007 2:59 PM
 

ASPInsiders said:

I am on way too many email lists. Between my IT career, space related sites, science R&D, Marine

October 12, 2007 5:45 PM
New Comments to this post are disabled

About Keith Barrows

I've been in computing since 1975. I started on an old PDP-8J with 3k of memory and 2 teletypes. I learned BASIC and Octal based assembly. I later moved into CPM, TR-DOS, Apple and finally into PC-DOS, Dr DOS and MS-DOS. I've been a beta tester for over a decade, got into web applications as a means to handle B2B requirements and have specialized in data movement between applications and businesses since. I have been a MVP, ASP Elite and was selected by Microsoft as one of the original 15 board members for ASPInsiders.

This Blog

Syndication

News

Disclaimer
About me

Locations of visitors to this page
weblogs.asp.net/kbarrows
BlogMailr Enabled <script type='text/javascript' src='http://track3.mybloglog.com/js/jsserv.php?mblID=2008010310421330'&gt;&lt;/script> <script type="text/javascript" src="http://pub.mybloglog.com/comm2.php?mblID=2008010310421330&amp;c_width=180&amp;c_sn_opt=y&amp;c_rows=5&amp;c_img_size=f&amp;c_heading_text=Recent+Readers&amp;c_color_heading_bg=005A94&amp;c_color_heading=ffffff&amp;c_color_link_bg=E3E3E3&amp;c_color_link=005A94&amp;c_color_bottom_bg=005A94"></script>
CS Build: 2.1.61129.2
1999
Listed on the CS Listings Powered By Community Server Themed by nb development