Consulting

Results 1 to 5 of 5

Thread: Finding folders while excluding a folder

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    69
    Location

    Finding folders while excluding a folder

    A few months ago, I obtained the code below here in the VBAExpress forum. The code is brilliant, but I would like to add one more thing to it if possible. On the network I have access to Public Folders (which I don't currently use). How can I get this code to exclude Public Folders in the search?

    Thanks

    Sub FindFolderByName()
    Dim Name As String
    Dim FoundFolder As Folder
    
    Name = InputBox("Find Name:", "Search Folder")
    If Len(Trim$(Name)) = 0 Then Exit Sub
    Set FoundFolder = FindInFolders(Application.Session.Folders, Name)
    If Not FoundFolder Is Nothing Then
    'If MsgBox("Activate Folder: " & vbCrLf & FoundFolder.FolderPath, vbQuestion Or vbYesNo) = vbYes Then
    Set Application.ActiveExplorer.CurrentFolder = FoundFolder
    'End If
    Else
    MsgBox "Not Found", vbInformation
    End If
    End Sub
    
    
    Function FindInFolders(TheFolders As Outlook.Folders, Name As String)
    Dim SubFolder As Outlook.MAPIFolder
    
    On Error Resume Next
    Set FindInFolders = Nothing
    
    For Each SubFolder In TheFolders
    Debug.Print SubFolder.Name
    If LCase(SubFolder.Name) Like "*" & LCase(Name) & "*" Then
    If MsgBox("Activate Folder: " & vbCrLf & SubFolder.FolderPath, vbQuestion Or vbYesNo) = vbYes Then
    Set FindInFolders = SubFolder
    Exit For
    Else
    ' If folder is rejected act as if it was never suggested.
    GoTo nextFolder
    End If
    Else
    nextFolder:
    Set FindInFolders = FindInFolders(SubFolder.Folders, Name)
    If Not FindInFolders Is Nothing Then Exit For
    End If
    Next
    End Function
    Last edited by SamT; 10-18-2017 at 06:21 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    bump
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    May 2016
    Posts
    69
    Location
    bump

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    you'll need the Private/Public Folder Attribute
    If the Found Folder has the wrong Attribute, GoTo the next one.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    You can reference any folder in the tree.


    Sub FindFolderByName()
    
        Dim Name As String
        Dim FoundFolder As folder
        
        Dim topFolder As folder
             
        Name = InputBox("Find Name:", "Search Folder")
        
        If Len(Trim$(Name)) = 0 Then Exit Sub
        
        ' All folders
        'topFolder = Session.folders
        
        ' or Mailbox associated with the default Inbox
        'topFolder = Session.GetDefaultFolder(olFolderInbox).Parent
        
        ' or default Inbox
        topFolder = Session.GetDefaultFolder(olFolderInbox)
    
        Set FoundFolder = FindInFolders(topFolder.folders, Name)
        
        If Not FoundFolder Is Nothing Then
            'If MsgBox("Activate Folder: " & vbCrLf & FoundFolder.FolderPath, vbQuestion Or vbYesNo) = vbYes Then
            Set Application.ActiveExplorer.CurrentFolder = FoundFolder
            'End If
        Else
            MsgBox "Not Found", vbInformation
        End If
        
    End Sub
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •