move email based on criteria - vba

VeryForgetful

Board Regular
Joined
Mar 1, 2015
Messages
242
Hi,

I'm trying to create some code to move email based on various criteria within my inbox.

The code needs to accomplish the following:


  • Move email from inbox to urgent subfolder based on senderemailaddress, listed in column A.
  • Move email from inbox to urgent subfolder based on a keyword found in senderemailaddress using a list of values located in column B. The code below attempts to find the name Smith in the address but it doesn't quite work for some reason.
  • Move email from inbox to urgent subfolder based on a keyword found in the email body using a list of values located in column C.

Code:
Sub UrgentEmail()

'Use the Reference command on the Visual Basic for Applications (VBA) Tools menu
'to set a reference to Microsoft Outlook xx.x Object Library,

    Dim olApp As Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim olInBox As Outlook.MAPIFolder
    Dim olMoveToFolder As Outlook.MAPIFolder
    Dim olItems As Outlook.Items

    Set olApp = CreateObject("Outlook.Application")
    Set olNS = olApp.GetNamespace("MAPI")
    Set olInBox = olNS.GetDefaultFolder(olFolderInbox)
    Set olMoveToFolder = olInBox.Folders("Urgent")    'change the name of the subfolder accordingly
    Set olItems = olInBox.Items
    For Each MyEmail In olItems
        If InStr(MyEmail.SenderEmailAddress, "smith") > 0 Then
            olItems(MyEmail).Move olMoveToFolder
        End If
    Next MyEmail
    Set olApp = Nothing
    Set olNS = Nothing
    Set olInBox = Nothing
    Set olMoveToFolder = Nothing
    Set olItems = Nothing
End Sub

Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,214,826
Messages
6,121,793
Members
449,048
Latest member
greyangel23

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