Outlook Macro - Compare words in Subject against Folder titles

wtnelso

Board Regular
Joined
Feb 15, 2012
Messages
241
I have been working on a macro to sort inbound email into folders depending on various factors. I had successfully implemented to macro, but am in the works of refining the algorithm to make it more precise. What I want to do now is compare the folder names against the words in the email subject line. So folder "My Folder" would be the destination of a subject line that stated "Hello this is My Folder subject line". I have it all figured out except the code to find the folder name in the subject line. Here is what I have so far in a public function.
Code:
Public Function checkFunction(ByVal messageSubject As String) As Boolean
    Dim match As Boolean
    Dim ns As Outlook.NameSpace
    Dim myfolder As Outlook.Folder
    Dim mysubfolder As Outlook.Folder
    Set ns = Application.GetNamespace("MAPI")
     'Get the default inboxfolder
    Set myfolder = ns.GetDefaultFolder(olFolderInbox)
     'Loop through each folder and display name of the folder
    For Each mysubfolder In myfolder.Folders
        MsgBox mysubfolder.Name 'Test of where code is.
        'If objSourceFolder.Item.Find(mysubfolder.Name, messageSubject) = True Then
            MsgBox "Here!" 'Test of where code is.
            match = True
        Else
            MsgBox "Not true" 'Test of where code is.
        End If
    Next mysubfolder
        
    checkFunction = match
End Function
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I believe the InStr function would be what you need:
Code:
    For Each mysubfolder In myfolder.Folders
        If InStr(mysubfolder.Name, messageSubject) > 0 Then
            MsgBox "Found match for " & mysubfolder.Name, vbOKOnly + vbInformation, "Found Match" 'Test of where code is.
            Match = True
            Exit For
        'Else
            'Nothing here, or you will see each non-match before the match is found
        End If
    Next mysubfolder
    If Match = False Then MsgBox "Found no match for " & mysubfolder.Name, vbOKOnly + vbCritical, "No Match Found" 'Test of where code is.

Also you may want to test loading all of the folder names into a public array one time when Outlook is opened rather than iterating all folders each time you run the function.

Match is a worksheet function, I would change it to bMatch to ensure no confusion in debugging.
 
Upvote 0

Forum statistics

Threads
1,225,681
Messages
6,186,411
Members
453,352
Latest member
OrionF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top