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.
 
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
M Search message, then (1) Jump to folder & (2) Select message that you searched for Outlook VBA and Custom Forms 7
G Search Folders and Jump to Folder Outlook VBA and Custom Forms 2
A Search folder and move the email Outlook VBA and Custom Forms 0
T How to find or display the sub-folder name for an Archive Search Using Outlook 10
P Search folder: all emails sent to or from a domain Using Outlook 1
Victor_50 Outlook 2019 Jump to folder from search folder Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
B Search and Find Email by Folder Name Outlook VBA and Custom Forms 2
P Posts in Folder No Longer Group by Conversation Column After Search Using Outlook 0
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
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 Using Outlook 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
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
A 'search people' now asks me to 'press enter' Using Outlook 11
A Relocate Search Bar in Outlook Using Outlook 2
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
C Advanced search terms for "Outlook Data File" Using Outlook 1
T Outlook365 search item listed as "potential matches" can't be opened Using Outlook 0
E Outlook 365 Save Selected Email Message as .msg File - oMail.Delete not working when SEARCH Outlook VBA and Custom Forms 0

Similar threads

Back
Top