Search for folder by key in subject then move new message to related folder

Status
Not open for further replies.

Erfan

New Member
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
I need to move the incoming message to the related folder if there is a loop for that depending on a key in the subject of the message.

I developed a script for getting the key in the subject of new message. How can I search rest of messages by a key and retrieve related folder?

Sub CustomMailMessageRule(Item As Outlook.MailItem)
Dim strTicket, strSubject As String
Dim strFolder As String
strTicket = "None"
strSubject = Item.Subject
If InStr(1, strSubject, "#-") > 0 Then
strSubject = Mid(strSubject, InStr(strSubject, "#-") + 2)
If InStr(strSubject, " ") > 0 Then
strTicket = Left(strSubject, InStr(strSubject, " ") - 1)
End If
End If
the unknown part, search all folders by key and retrieve the related folder

strFolder = "???"
and finally, move the incoming message to the related folder by below code

If InStr(strFolder) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).folders(strFolder)

MsgBox "Your New Message has been moved to related folder "
End Sub
I'm new in VBA.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
If you have many folders, it will be slow... better is to use a double array - one for the keyword and one for the matching folder. The exception is if the keyword is the folder name...

i used this in a macro to file into a folder whose name was the keyword

Code:
Public Sub MoveMessages(ByVal Item As MailItem)
' get the string
' code goes here

' if the code is in the message, find the folder
' move message
    FindFolder
    On Error Resume Next
    Item.UnRead = True
    Item.Move m_Folder
      If m_Folder Is Nothing Then
        Exit Sub
      End If

Err.Clear
End Sub

' Borrowing Michael's code from
' http://vboffice.net/en/developers/find-folder-by-name

Public Sub FindFolder()
  Dim Name$
  Dim Folders As Outlook.Folders
  Dim Folder As Outlook.MAPIFolder

  Set m_Folder = Nothing
  m_Find = ""

  Name = "*" & strCode
  If Len(Trim$(Name)) = 0 Then Exit Sub
  m_Find = Name

  m_Find = LCase$(m_Find)

  Set Folder = Session.GetDefaultFolder(olFolderInbox)
  LoopFolders Folder.Folders

End Sub

Private Sub LoopFolders(Folders As Outlook.Folders)
  Dim Folder As Outlook.MAPIFolder
  Dim F As Outlook.MAPIFolder
  Dim Found As Boolean
  If SpeedUp = False Then DoEvents

  For Each F In Folders
      Found = (LCase$(F.Name) Like m_Find)

    If Found Then
      Set m_Folder = F
      Exit For
    Else
      LoopFolders F.Folders
      If Not m_Folder Is Nothing Then Exit For
    End If
  Next
End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
S Create A Search Folder That Looks For Message Class? Outlook VBA and Custom Forms 0
R Search/Jump to a folder by typing its name Outlook VBA and Custom Forms 1
M Sent mail not showing in Sent Items folder; but they can be found with Search Using Outlook 3
W Create Search Folder excluding Specific Email Addresses Using Outlook 5
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
A Search folder refresh Outlook VBA and Custom Forms 3
F Search folder for tasks in all task folders Using Outlook 1
B Search: Cannot find which Folder Contains a Message Using Outlook 3
S VBA Code to move mail items from search folder to inbox subfolder Outlook VBA and Custom Forms 4
S Auto move search results to folder Outlook VBA and Custom Forms 0
S VBA to search a keyword in attachment and move to a folder Outlook VBA and Custom Forms 0
Diane Poremsky Use VBA to create an Outlook Search Folder for Sender New Slipstick.com Articles 0
B Search Folder for search messages in more than one account and / or multiple PST folders Using Outlook 3
Van Fog Get items in search folder for shared mailbox Outlook VBA and Custom Forms 3
M Is there any easy way to quickly search for the name of a folder in Outlook's inbox folder structure Exchange Server Administration 1
R Inbox search, current folder, it's in there-but not found. Using Outlook 2
C search outlook 2013 folder using keyword identifiers Using Outlook 1
S Contacts disappear in public folder search Using Outlook 1
L Save Contact Search as a Folder 2007 Using Outlook 1
Mark Rideout Search folder for appointments and emails Using Outlook 2
Mark Rideout Search Folder of flagged messages including "tracked conversations" Using Outlook 2
Jennifer Murphy How to search a folder and its subfolders Using Outlook 6
L Outlook 2007 Search Folder Using Outlook 1
I Outlook 2010 Search Folder criteria for all mails to and from external domain Using Outlook 9
R Flagged for Follow Up Search folder MIA after PST export/import Using Outlook 1
D Outlook Search Folder Limitation Using Outlook 1
F All mail to/from a doman with a Search Folder Using Outlook 3
W Go to search field when entering a contact folder? Using Outlook 2
T Permanent Removal of RSS feeds folder amd Search folder in Outlook 2010 Using Outlook 10
T Custom Search Folder Unread Using Outlook 4
H Search for a FOLDER Using Outlook 2
R how to create a search folder with multiple categoreis as citeria Using Outlook 4
D Starting Outlook - Stuck on processing, search folder as default "start Outlook in this folder" Using Outlook 1
D Outlook 2010 Moving messages collected in a search folder with Find Messages from Sender Using Outlook 1
M Is there a RefreshNow action for Search Folder? Outlook VBA and Custom Forms 8
M Create search folder filter that converts UTC time to local? Outlook VBA and Custom Forms 9
M How to programmatically select a outlook search folder? Outlook VBA and Custom Forms 1
S Loop mail items within a Custom Search Folder Outlook VBA and Custom Forms 1
E How to speed up a search on a shared mailbox folder Outlook VBA and Custom Forms 5
P outlook 2008 search box criteria couldn't be saved Using Outlook 2
C Outlook with Office365 - search across account, by date rate, in multiple folders - how? Using Outlook 2
D Custom Search Folders not refreshing/updating automatically Using Outlook 0
L New Location for the Search Bar Using Outlook 6
T How can you include Junk Email in Search Results like you can include Deleted Items? Using Outlook 3
J Outlook 2016 After a search in all mailboxes, where is each message that was found? Using Outlook 6
King Mustard Sort search groups by amount of items? Using Outlook 1
V Outlook 2016 will not move emails in search results Using Outlook 4
T Search for incoming e-mails for a specified time range Using Outlook 1
S outlook 2007 calendar search Using Outlook 6
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6

Similar threads

Top