Search folder and move the email


New Member
Outlook version
Outlook 2016 32 bit
Email Account (as MS Exchange)
Hi All,

I found this very useful macro from user Nymphe1410, on another form, and it has been helping my workflow for over 2 weeks but it is no longer working. Do you have any suggestions on how I can fix this?

Purpose: The purpose of this macro is to move an email to a found folder that is nestled inside a folder one of my folders inside my inbox. The macro would allow me to search for the folder then go to the folder or move the email (or emails) and go to the folder.

This code was been working for me for 2 weeks but all of a sudden it no longer works. Note: My trust center has macros enabled, I restarted the program, deleted the macro, and even changed the project name. The window would pop up to enter the folder I want to find but it does not go past that.

Option Explicit
Private m_Folder As Outlook.MAPIFolder
Private m_Find As String
Private m_Wildcard As Boolean

Private Const SpeedUp As Boolean = False
Private Const StopAtFirstMatch As Boolean = True

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

'additons for move to folder
Dim objNS As Outlook.NameSpace
Dim objItem As Outlook.MailItem
Set objNS = Application.GetNamespace("MAPI")
Set objItem = Application.ActiveExplorer.Selection.Item(1)
'additions for move to folder

Set m_Folder = Nothing
m_Find = ""
m_Wildcard = False

Name = InputBox("Find folder by name:", "Search folder & Move Item")
If Len(Trim$(Name)) = 0 Then Exit Sub
m_Find = "*" & Name & "*" '<--- good addition so that we don't need to add * everytime.

m_Find = LCase$(m_Find)
m_Find = Replace(m_Find, "%", "*")
m_Wildcard = (InStr(m_Find, "*"))

Set Folders = Application.Session.Folders
LoopFolders Folders

If Not m_Folder Is Nothing Then

If MsgBox("Activate folder or just move the item to it: " & vbCrLf & vbCrLf & m_Folder.FolderPath & vbNewLine & vbNewLine & "Yes = Activate the folder only" & vbNewLine & "No = Move the item and activate", vbQuestion Or vbYesNo) = vbYes Then

'only activate the folder:

Set Application.ActiveExplorer.CurrentFolder = m_Folder

' move the item to the found folder and activate to be sure:

objItem.Move m_Folder '<-- where magic happens :)

Set Application.ActiveExplorer.CurrentFolder = m_Folder '<-- this line can be deactivated if not needed.

End If
MsgBox "Not found", vbInformation
End If
End Sub

Private Sub LoopFolders(Folders As Outlook.Folders)
Dim F As Outlook.MAPIFolder
Dim Found As Boolean

If SpeedUp = False Then DoEvents

For Each F In Folders
If m_Wildcard Then
Found = (LCase$(F.Name) Like m_Find)
Found = (LCase$(F.Name) = m_Find)
End If

If Found Then
If StopAtFirstMatch = False Then
If MsgBox("Found: " & vbCrLf & F.FolderPath & vbCrLf & vbCrLf & "Continue?", vbQuestion Or vbYesNo) = vbYes Then
Found = False
End If
End If
End If
If Found Then
Set m_Folder = F
Exit For
LoopFolders F.Folders
If Not m_Folder Is Nothing Then Exit For
End If
End Sub
Similar threads
Thread starter Title Forum Replies Date
E Search for folder by key in subject then move new message to related folder Outlook VBA and Custom Forms 1
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
M Search message, then (1) Jump to folder & (2) Select message that you searched for Outlook VBA and Custom Forms 6
G Search Folders and Jump to Folder Outlook VBA and Custom Forms 2
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
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