Search/Jump to a folder by typing its name

reubendayal

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
 
Top