VBA CODE Help? or point me in the right direction.

GaryG9595

Board Regular
Joined
Jun 13, 2014
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Hello,
Does anyone know of a way to move emails (outlook) from one subfolder to another based on a list from excel?
Similar to a rule, but instead of adding the specific words/numbers one at a time, can I use a list from excel?
Basically, I have a list of completed items in Column R in excel and would like to find the "20531" in the subject line of my outlook folder "Update Contact Info"and if it's there, move it to another folder called "Completed" and leave the remaining emails.
Thanks,
Gary


1657303369844.png
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi Gary,
Give this sample code a try. I assume both folders are created under the Inbox.


VBA Code:
Sub GetOutlookReceivedEmail()
    Const olFolderInbox As Long = 6
    Dim OutlookApp As Object, myNamespace As Object
    Dim oFromFolder As Object, oToFolder As Object
    Const startRow As Long = 5    'Data starting row in Column R
    Dim LastRow As Long
    Dim sTitle As String
    Dim Ret As Variant
    Dim i As Long

    'Set Outlook instance and NameSpace
    Set OutlookApp = CreateObject("Outlook.Application")
    Set myNamespace = OutlookApp.GetNamespace("MAPI")

    'Set Target Folders (Assume these are placed under "inbox")
    Set oFromFolder = myNamespace.GetDefaultFolder(olFolderInbox).Folders.Item("Update Contact Info")
    Set oToFolder = myNamespace.GetDefaultFolder(olFolderInbox).Folders.Item("Completed")

    'Looping for checking and moving items

    LastRow = Cells(Rows.Count, "R").End(xlUp).Row
    For i = oFromFolder.Items.Count To 1 Step -1
        sTitle = oFromFolder.Items.Item(i).Subject 'Title of email
        'Check if it's exist in the list in Column R
        Ret = Application.Match(sTitle, Cells(startRow, "R").Resize(LastRow - startRow + 1).Value, 0)
        'If Exist, move it.
        If Not IsError(Ret) Then oFromFolder.Items.Item(i).Move oToFolder
    Next
    MsgBox "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,539
Members
449,316
Latest member
sravya

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