How to move emails from Outlook sent items to other folder based on mail subject using vba code? Thanks

310 Views Asked by At

I want to move my emails from sent items to another folder based on the subject contains "Drive 20-Feb-23" using vba code.

I want to move my emails from sent items to another folder based on the subject contains "Drive 20-Feb-23" using vba code.

2

There are 2 best solutions below

0
Eugene Astafiev On

You need to use the Move method which moves a Microsoft Outlook item to a new folder.

To find items with a specified subject you can use the Find/FindNext or Restrict methods of the Items class. The simplest DASL syntax may look like that:

sFilter = "[Subject] = 'Drive 20-Feb-23'"

But a better approach would be to use a substring matching mechanism instead:

criteria = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " ci_phrasematch 'Drive 20-Feb-23'" 

Read more about these methods in the articles that I wrote for the technical blog:

0
Dmitry Streblechenko On

Do you want the messages to go to a different folder immediately after you send them? Trap the Application.ItemSend event (the item being sent will be passed to your handler), check the subject or any other appropriate condition, and set the MailItem.SaveSentMessageFolder property to the appropriate folder from the same (!) store.