Search/Jump to a folder by typing its name

Status
Not open for further replies.

reubendayal

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
Hi All,

Under one of my mail boxes, I have several client folders as a sub sub folder. And it is a bit time consuming when working on the laptop screen to scroll up and down to locate the folder and archive the emails to it or even open it and work on the emails from it. I have looked around and found the below code seems to do an okay job. And I have added the source folder from which the code should search under. However, it seems to hang outlook for a few seconds (20-40 seconds) each time I use it which simply ends up being way too slow and not very useful.

Could you help me clean up the code to make it work faster. or suggest another way of searching through the folder names to find the case folder by its name and then open it?

Thanks so much!

Code:
'****Finding folders by name****

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

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

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

  Set m_Folder = Nothing
  m_Find = ""
  m_Wildcard = False

  Name = InputBox("Find name:", "Search folder")
  If Len(Trim$(Name)) = 0 Then Exit Sub
  m_Find = Name

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

  Set Folders = Application.Session.Folders("DK Maerskimmigration").Folders("Inbox").Parent.Folders("Assignees 2019") 'Application.Session.Folders
  LoopFolders Folders

  If Not m_Folder Is Nothing Then
    If MsgBox("Activate folder: " & vbCrLf & m_Folder.FolderPath, vbQuestion Or vbYesNo) = vbYes Then
      Set Application.ActiveExplorer.CurrentFolder = m_Folder
    End If
  Else
    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)
    Else
      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
    Else
      LoopFolders F.Folders
      If Not m_Folder Is Nothing Then Exit For
    End If
  Next
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Are you searching a shared mailbox? That could slow it down.

>> Set Folders = Application.Session.Folders("DK Maerskimmigration").Folders("Inbox").Parent.Folders("Assignees 2019")
I'm not sure if it will speed it up much, but you have a lot of dots and that can be slower - splitting the lines is recommended. It's usually recommended to split each - level in the path to a variable, but I usually do it like this.

Set ParentPath = Application.Session.Folders("DK Maerskimmigration").Folders("Inbox")
Set Folders = ParentPath.Parent.Folders("Assignees 2019")
or
Set Folders = Application.Session.Folders("DK Maerskimmigration").Folders("Inbox")
Set Folders = Folders.Parent.Folders("Assignees 2019")

Do the folders use the users name in First Last format? (Or last first) ? I have a macro that I think is fast but it doesn't ask for the folder name - actually, it could - but if there is a way to identify the folder from the display name or message, it might be faster. Then ask for the name only if its not found. (It could look up the contact by email address and use the contact display name but that would slow it down - although it might not be any slower than typing the name.)

This is the search portion of my macro. It's basically the same but i removed the code that asks if its the right folder since we will only have one match. It looks for a client code in the subject and body and the folders use the client code, not the client name. (Which means they need to code all mail coming and going.)



Code:
' 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).Parent.Folders("Clients")
  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.
Thread starter Similar threads Forum Replies Date
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 1
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
J Message search and edit, another way? Outlook VBA and Custom Forms 4
S Create A Search Folder That Looks For Message Class? Outlook VBA and Custom Forms 0
R Outlook for Mac - Advanced Search and Tracking Sent Emails Using Outlook 0
M Sent mail not showing in Sent Items folder; but they can be found with Search Using Outlook 3
A Outlook 2019 having issues with 'people search' and not making suggestions in email To: etc Using Outlook 12
W Search Incomplete Outlook 2016 Using Outlook 5
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 4
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 0
A Using or not using apostrophes in search terms has this changed? Using Outlook 0
R outlook address book search broken Using Outlook 2
K Outlook.com is not showing, or finding via search, all my Contacts (People) Using Outlook 0
W Create Search Folder excluding Specific Email Addresses Using Outlook 5
R What's supposed to appear when just clicking into the search field? Using Outlook 7
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
M possible to search Outlook for multiple email addresses at once? Using Outlook 1
B Instant search with special characters Using Outlook 1
A Search folder refresh Outlook VBA and Custom Forms 3
B Advanced Search in MS Outlook by VBA and SQL Outlook VBA and Custom Forms 2
M email search not working Using Outlook 1
F Search folder for tasks in all task folders Using Outlook 1
W Outlook 2016 search problem persists after applying all known solutions Using Outlook 12
B Looking to filter (or just find/search) for only messages that the sender has sent more than 1 messa Using Outlook 2
B Search: Cannot find which Folder Contains a Message Using Outlook 3
M how to speed up search of shared contacts Using Outlook 0
B When I add more search strings to RULES, it is not processing them Using Outlook 3
C Search with Google Macro? Outlook VBA and Custom Forms 4
E Search for folder by key in subject then move new message to related folder Outlook VBA and Custom Forms 1
E Outlook 2016 Inbox search bug Using Outlook 1
C Filter/Search emails sent to internal Exchange address only Using Outlook 2
M VBA macro for Inbox's attachments search Outlook VBA and Custom Forms 0
P Outlook search is not working at all for Calendar & Tasks Using Outlook 7
M Recent Update Did not Fix Search Problems Using Outlook 7
Commodore Search cannot complete the indexing of your Outlook data Using Outlook 6
M new search just... sucks Using Outlook 4
B Search function in Office 365 has stopped working Using Outlook 3
J Marco in search of text in subject line Using Outlook 8
T Delete Emails from search results Outlook VBA and Custom Forms 1
P Can't search Archive'd Tasks Using Outlook 0
S VBA Code to move mail items from search folder to inbox subfolder Outlook VBA and Custom Forms 4
Similar threads


















































Top